簡単 4件解決済み 正答率80.0%

Point Quadrant

Find which quadrant a point lies in.

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

  • Check the signs of x and y
  • Handle points on the axes
  • Combine two conditions

問題

翻訳を表示

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

First determine whether the point lies on an axis or at the origin.

ヒント 2

If it is not on an axis, the signs of x and y tell you the quadrant.

ヒント 3

The origin is a special case, and axis points are not in any quadrant.

目安の計算量

O(1) time, O(1) extra space

参考資料

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

ランキング

全体ランキング

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

# コーダー 言語 時間 メモリ AI 評価 解答
1 ДЗДавид Зайдулин Python 968 ms - 5/5 コードを見る
2 HJhettory Jurash Python 976 ms - 2/5 コードを見る
3 LMLaura Mahlow Python 1,003 ms - 5/5 コードを見る
4 PKPornchai Kaewkrabil Go 1,354 ms - 4/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

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