题目传送门:https://pintia.cn/problem-sets/994805260223102976/problems/994805274496319488
题解:
/* 对于本题,有一个坑:当输出的A或B小于0.005时,应该为0 cout << setiosflags(ios::fixed) << setprecision(2) << ???这个在保留的时候会自动四舍五入,如A为0.006时,如果不用上述条件判断,此格式输出就是0.01,而正确答案应该为0.00 */ #include<iostream> #include<cmath> #include<iomanip> #include<algorithm> using namespace std; int main() { double R1, P1, R2, P2, ans_A, ans_B; cin >> R1 >> P1 >> R2 >> P2; ans_A = R1 * R2*cos(P1)*cos(P2) - R1 * R2*sin(P1)*sin(P2); ans_B = R1 * cos(P1)*R2*sin(P2) + R1 * sin(P1)*R2*cos(P2); if (abs(ans_A) < 0.005) ans_A = 0; cout << setiosflags(ios::fixed) << setprecision(2) << ans_A; if (abs(ans_B) < 0.005) ans_B = 0; if (ans_B >= 0) cout << "+"; cout << setiosflags(ios::fixed) << setprecision(2) << ans_B; cout << "i"; return 0; }