簡単 1件解決済み 正答率100.0%

Point Quadrant

Find which quadrant a point lies in.

このチャレンジで学べること

  • x と y の符号を調べる
  • 軸上の点を扱う
  • 2 つの条件を組み合わせる

問題

The x and y axes split the plane into four quadrants: Q1 (x > 0, y > 0), Q2 (x < 0, y > 0), Q3 (x < 0, y < 0) and Q4 (x > 0, y < 0). Points on an axis belong to no quadrant.

Implement point_quadrant(x, y).

Input

x: The x coordinate. y: The y coordinate.

Output

Return "Q1", "Q2", "Q3" or "Q4"; "X-axis" or "Y-axis" for points on an axis; or "Origin" for (0, 0).

制約

-1,000,000 ≤ x, y ≤ 1,000,000

例

例 1
入力:
x = -3
y = 4

期待される出力:
expected = "Q2"
入力:
long long x = -3;
long long y = 4;

期待される出力:
std::string expected = "Q2";
入力:
long x = -3;
long y = 4;

期待される出力:
string expected = "Q2";

解説

x is negative and y is positive, so the point is in Q2.

例 2
入力:
x = 5
y = 0

期待される出力:
expected = "X-axis"
入力:
long long x = 5;
long long y = 0;

期待される出力:
std::string expected = "X-axis";
入力:
long x = 5;
long y = 0;

期待される出力:
string expected = "X-axis";

解説

y is 0, so the point lies on the X-axis.

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

まず、その点が軸上か原点かを判定する。

ヒント 2

軸上でなければ、x と y の符号で象限が決まる。

ヒント 3

原点は特別扱いで、軸上の点はどの象限にも入らない。

目安の計算量

時間 O(1)、追加メモリ O(1)

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 LMLaura Mahlow Python 1,003 ms - 5/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意:下に1件の全解答あり まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。