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 资源

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。