Python 基礎知識簡單

給定座標所在的象限

「給定座標所在的象限」問題的詳細指南和 Python 實作。

問題陳述

簡單

寫一個函數 find_quadrant(x, y),它採用兩個整數 xy 表示笛卡爾平面上的一個點,並傳回一個字串,指示該點位於哪個象限。

返回:

- 如果 x > 0 且 y > 0,則為“象限 1”

- 如果 x < 0 且 y > 0,則為“象限 2”

- 如果 x < 0 且 y < 0,則為“象限 3”

- 如果 x > 0 且 y < 0,則為“象限 4”

-「原點」如果 x == 0 且 y == 0

- 'X 軸' 如果 y == 0 且 x != 0

- 'Y 軸' 如果 x == 0 且 y != 0

約束條件
  • -10^4 <= x, y <= 10^4

範例

Example 1
Input
find_quadrant(3, 5)
Output
'Quadrant 1'
Explanation

Both x=3 and y=5 are positive, so the point is in Quadrant 1 (upper-right).

Example 2
Input
find_quadrant(-2, 4)
Output
'Quadrant 2'
Explanation

x=-2 is negative and y=4 is positive, so the point is in Quadrant 2 (upper-left).

Example 3
Input
find_quadrant(-1, -7)
Output
'Quadrant 3'
Explanation

Both x=-1 and y=-7 are negative, so the point is in Quadrant 3 (lower-left).

Need a Hint?
考慮使用特定於數字的資料結構,例如集合或堆。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在編輯器中開啟
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。