Đề bài
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).
Ràng buộc
-1,000,000 ≤ x, y ≤ 1,000,000
Ví dụ
Đầu vào:
x = -3
y = 4
Kết quả mong đợi:
expected = "Q2"
Đầu vào:
long long x = -3;
long long y = 4;
Kết quả mong đợi:
std::string expected = "Q2";
Đầu vào:
long x = -3;
long y = 4;
Kết quả mong đợi:
string expected = "Q2";
Giải thích
x is negative and y is positive, so the point is in Q2.
Đầu vào:
x = 5
y = 0
Kết quả mong đợi:
expected = "X-axis"
Đầu vào:
long long x = 5;
long long y = 0;
Kết quả mong đợi:
std::string expected = "X-axis";
Đầu vào:
long x = 5;
long y = 0;
Kết quả mong đợi:
string expected = "X-axis";
Giải thích
y is 0, so the point lies on the X-axis.
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
First determine whether the point lies on an axis or at the origin.
Gợi ý 2
If it is not on an axis, the signs of x and y tell you the quadrant.
Gợi ý 3
The origin is a special case, and axis points are not in any quadrant.
Độ phức tạp mong đợi
O(1) time, O(1) extra space
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.