当前位置:网站首页>PAT B1051

PAT B1051

2022-06-25 19:56:00 Madness makes freedom

1051 Complex multiplication (15 branch )

The plural can be written as (A+Bi) The normal form of , among A It's the real part ,B It's the empty part ,i Imaginary units , Satisfy i2=−1; It can also be written as an exponential form in polar coordinates (R×e(Pi)), among R It's a complex module ,P It's the angle ,i Imaginary units , It's equivalent to the trigonometric form R(cos(P)+isin(P)).

Now let's give two complex numbers R and P, The normal form of the product of two numbers is required .

Input format :

Enter two complex numbers in a row R1​, P1​, R2​, P2​, Numbers are separated by spaces .

Output format :

In a row, press A+Bi The normal form of the product of two numbers , Both the real part and the virtual part are reserved 2 Decimal place . Be careful : If B It's a negative number , It should be written as A-|B|i In the form of .

sample input :

2.3 3.5 5.2 0.4

sample output :

-8.68-8.23i

For the result 0 When , To make a special judgment , Because a very small decimal is rounded to the nearest 0 When , It could be a positive number , It can also be negative , When it's negative , We can't output this minus sign !!!!

Regarding this , I wrote two codes !

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
    double r1,p1,r2,p2;
    cin >> r1 >> p1 >> r2 >> p2;
    double r=r1*r2,p=p1+p2;
    double x=r*cos(p),y=r*sin(p);
    if(x+0.005>0&&x<0)
        printf("0.00");
    else
        printf("%.2f",x);
    if(y+0.005>0&&y<0)
        printf("+0.00i");
    else if(y<0)
        printf("-%.2fi\n",fabs(y));
    else
        printf("+%.2fi\n",y);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
    double r1,p1,r2,p2;
    cin >> r1 >> p1 >> r2 >> p2;
    double x1=r1*cos(p1),y1=r1*sin(p1),x2=r2*cos(p2),y2=r2*sin(p2);
    double x=x1*x2-y1*y2,y=x1*y2+x2*y1;
    if(x+0.005>0&&x<0)
        printf("0.00");
    else
        printf("%.2f",x);
    if(y+0.005>0&&y<0)
        printf("+0.00i\n");
    else if(y<0)
        printf("-%.2fi\n",fabs(y));
    else
        printf("+%.2fi\n",y);
    return 0;
}

原网站

版权声明
本文为[Madness makes freedom]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202190512261175.html