要求:
写一个程序把极坐标(r,θ) (θ之单位为度)转换为直角坐标( X,Y)。转换公式是:
x=r.cosθ
y=r.sinθ
样例输入1:10 45(代表r=10 θ=45°)
样例输出1:7.071068 7.071068
样例输入2:20 90 (代表r=20 θ=90°)
样例输出2:0 20(可以接近似的结果)
[参考解答]
#include <stdio.h> #include <math.h> //三角函数需要math.h #define PI 3.1415926 //用符号常量处理 int main( ) { float r, theta, x, y; scanf("%f %f", &r, &theta); x = r * cos(theta/180*PI);//注意cos需要弧度作为参数 y = r * sin(theta/180*PI); printf("%f %f", x, y); return 0; }