記錄一下問題。
v1(13分)
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
#define ll long long
int main(){
double r1,p1,r2,p2;
cin>>r1>>p1>>r2>>p2;
double a1=r1*cos(p1);
double b1=r1*sin(p1);
double a2=r2*cos(p2);
double b2=r2*sin(p2);
double c1=a1*a2-b1*b2;
double c2=a1*b2+b1*a2;
if(c2<0){
printf("%.2lf-%.2lfi",c1,fabs(c2));
}else{
printf("%.2lf+%.2lfi",c1,c2);
}
return 0;
}
修正,當浮點數是負數的時候並且絕對值小於0.005,那麼輸出-0,實際上應該取0,所以增加一個判斷。如果正數無所謂反正輸出本來就是0。
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
#define ll long long
int main(){
double r1,p1,r2,p2;
cin>>r1>>p1>>r2>>p2;
double a1=r1*cos(p1);
double b1=r1*sin(p1);
double a2=r2*cos(p2);
double b2=r2*sin(p2);
double c1=a1*a2-b1*b2;
double c2=a1*b2+b1*a2;
if(fabs(c1)<0.01) c1=0;
if(fabs(c2)<0.01) c2=0;
if(c2<0){
printf("%.2lf-%.2lfi",c1,fabs(c2));
}else{
printf("%.2lf+%.2lfi",c1,fabs(c2));
}
return 0;
}