1051 复数乘法

记录一下问题。
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;
}
posted @ 2024-04-18 16:17  YuKiCheng  阅读(8)  评论(0编辑  收藏  举报