2 条题解

  • 0
    @ 2026-3-26 17:44:00

    NUMPY044 题解:笛卡尔转极坐标

    题目

    将 10×2 的笛卡尔坐标转换为极坐标。

    解题思路

    使用 np.sqrt() 计算半径,np.arctan2() 计算角度。

    代码

    import numpy as np
    Z = np.random.random((10, 2))
    X, Y = Z[:, 0], Z[:, 1]
    R = np.sqrt(X**2 + Y**2)
    T = np.arctan2(Y, X)
    print(R)
    print(T)
    

    代码详解

    笛卡尔坐标 vs 极坐标

    笛卡尔坐标 (x, y)     极坐标 (r, θ)
       x                     r: 到原点的距离
       y                     θ: 与 x 轴的夹角(弧度)
    

    转换公式

    • 半径:r=x2+y2r = \sqrt{x^2 + y^2}
    • 角度:θ=arctan2(y,x)\theta = \arctan2(y, x)

    示例

    假设 x=1, y=1:
    r = sqrt(1² + 1²) = sqrt(2) ≈ 1.414
    θ = arctan2(1, 1) = π/4 ≈ 0.785
    

    arctan vs arctan2

    # arctan:只能处理第一象限
    np.arctan(1)  # 0.785
    
    # arctan2:可以处理所有象限
    np.arctan2(1, 1)   # 0.785 (第一象限)
    np.arctan2(-1, 1)  # -0.785 (第四象限)
    np.arctan2(1, -1)  # 2.356 (第二象限)
    

    核心知识点

    1. np.sqrt() —— 平方根
    2. np.arctan2() —— 四象限反正切
    3. 极坐标表示:(r, θ)
    • 0
      @ 2026-3-25 15:38:35

      NumPy044 题解

      参考代码

      import numpy as np
      Z = np.array(list(map(int, input().split())))
      print(Z[::-1])
      
      • 1

      信息

      ID
      95
      时间
      1000ms
      内存
      256MiB
      难度
      (无)
      标签
      (无)
      递交数
      0
      已通过
      0
      上传者