题目大意:略。
解题思路:double round(double num,unsigned int bits) 构造 或 找到临界值 0.05 来控制 if...else...;round 函数IDE可能不会提示,但是是可以用的,需要引入math头文件。
AC 代码
#include<bits/stdc++.h> #include<cmath> #define mem(a,b) memset(a,b,sizeof a) #define INF 0x3f3f3f3f using namespace std; typedef long long ll; double a,b,c,d,rs1,rs2; double round(double num,unsigned int bits) { stringstream ss; ss<<fixed<<setprecision(bits)<<num; ss>>num; return num; } void print(double rs1,double rs2,char op) { if(fabs(rs1)<1e-2 && fabs(rs2)<1e-2) { if(b<0 && d<0) printf("(%.1lf%.1lfi) %c (%.1lf%.1lfi) = 0.0",a,b,op,c,d); else if(b<0) printf("(%.1lf%.1lfi) %c (%.1lf+%.1lfi) = 0.0",a,b,op,c,d); else if(d<0) printf("(%.1lf+%.1lfi) %c (%.1lf%.1lfi) = 0.0",a,b,op,c,d); else printf("(%.1lf+%.1lfi) %c (%.1lf+%.1lfi) = 0.0",a,b,op,c,d); } else if(fabs(rs1)<1e-2) { if(b<0 && d<0) printf("(%.1lf%.1lfi) %c (%.1lf%.1lfi) = %.1lfi",a,b,op,c,d,rs2); else if(b<0) printf("(%.1lf%.1lfi) %c (%.1lf+%.1lfi) = %.1lfi",a,b,op,c,d,rs2); else if(d<0) printf("(%.1lf+%.1lfi) %c (%.1lf%.1lfi) = %.1lfi",a,b,op,c,d,rs2); else printf("(%.1lf+%.1lfi) %c (%.1lf+%.1lfi) = %.1lfi",a,b,op,c,d,rs2); } else if(fabs(rs2)<1e-2) { if(b<0 && d<0) printf("(%.1lf%.1lfi) %c (%.1lf%.1lfi) = %.1lf",a,b,op,c,d,rs1); else if(b<0) printf("(%.1lf%.1lfi) %c (%.1lf+%.1lfi) = %.1lf",a,b,op,c,d,rs1); else if(d<0) printf("(%.1lf+%.1lfi) %c (%.1lf%.1lfi) = %.1lf",a,b,op,c,d,rs1); else printf("(%.1lf+%.1lfi) %c (%.1lf+%.1lfi) = %.1lf",a,b,op,c,d,rs1); } else { if(rs2<0) { if(b<0 && d<0) printf("(%.1lf%.1lfi) %c (%.1lf%.1lfi) = %.1lf%.1lfi",a,b,op,c,d,rs1,rs2); else if(b<0) printf("(%.1lf%.1lfi) %c (%.1lf+%.1lfi) = %.1lf%.1lfi",a,b,op,c,d,rs1,rs2); else if(d<0) printf("(%.1lf+%.1lfi) %c (%.1lf%.1lfi) = %.1lf%.1lfi",a,b,op,c,d,rs1,rs2); else printf("(%.1lf+%.1lfi) %c (%.1lf+%.1lfi) = %.1lf%.1lfi",a,b,op,c,d,rs1,rs2); } else { if(b<0 && d<0) printf("(%.1lf%.1lfi) %c (%.1lf%.1lfi) = %.1lf+%.1lfi",a,b,op,c,d,rs1,rs2); else if(b<0) printf("(%.1lf%.1lfi) %c (%.1lf+%.1lfi) = %.1lf+%.1lfi",a,b,op,c,d,rs1,rs2); else if(d<0) printf("(%.1lf+%.1lfi) %c (%.1lf%.1lfi) = %.1lf+%.1lfi",a,b,op,c,d,rs1,rs2); else printf("(%.1lf+%.1lfi) %c (%.1lf+%.1lfi) = %.1lf+%.1lfi",a,b,op,c,d,rs1,rs2); } } puts(""); } int main() { while(~scanf("%lf%lf%lf%lf",&a,&b,&c,&d)) { rs1=round(a+c,1); rs2=round(b+d,1); print(rs1,rs2,'+'); rs1=round(a-c,1); rs2=round(b-d,1); print(rs1,rs2,'-'); rs1=round(a*c-b*d,1); rs2=round(b*c+a*d,1); print(rs1,rs2,'*'); rs1=round((a*c+b*d)/(c*c+d*d),1); rs2=round((b*c-a*d)/(c*c+d*d),1); print(rs1,rs2,'/'); } return 0; }