複數可以寫成 (A+Bi) 的常規形式,其中 A 是實部,B 是虛部,i 是虛數單位,滿足 i2=−1;也可以寫成極座標下的指數形式 (R×e(Pi)),其中 R 是複數模,P 是輻角,i 是虛數單位,其等價於三角形式 (R(cos(P)+isin(P))。
現給定兩個複數的 R 和 P,要求輸出兩數乘積的常規形式。
輸入格式:
輸入在一行中依次給出兩個複數的 R1, P1, R2, P2,數字間以空格分隔。
輸出格式:
在一行中按照 A+Bi
的格式輸出兩數乘積的常規形式,實部和虛部均保留 2 位小數。注意:如果 B
是負數,則應該寫成 A-|B|i
的形式。
輸入樣例:
2.3 3.5 5.2 0.4
輸出樣例:
-8.68-8.23i
#include<iostream>
#include<math.h>
using namespace std;
int main(){
while(1){
double r1,p1,r2,p2;
double x,y;
scanf("%lf%lf%lf%lf",&r1,&p1,&r2,&p2);
x=r1*r2*(cos(p1)*cos(p2)-sin(p1)*sin(p2));
y=r1*r2*(sin(p1)*cos(p2)+cos(p1)*sin(p2));
if(x>-0.005&&x<0.005)////核心
x=+0;
if(y>-0.005&&y<0.005)
y=+0;
if(y>=0)
printf("%0.2lf+%0.2lfi",x,y);
else
printf("%0.2lf%0.2lfi",x,y);
}
}