PAT B -1051 complex multiplier (15 minutes)

Click on the link full solution summary PAT B -AC

Title:
complex can be written as (A + Bi) of conventional form, where A is a real part, B is the imaginary unit, i is the imaginary unit, satisfies I 2 = -1; can also be written in polar coordinates exponential (R E × (Pi) ), where R is the complex modulus, P is the convergence angle, i is the imaginary unit, which is equivalent to a triangular form R (cos (P) + isin (P)).

It is now given of two complex numbers R and P, the number of required output the product of two conventional forms.

Input format:
sequentially input plural given two in a row R & lt . 1 , P . 1 , R & lt 2 , P 2 , separated by a space between digits.

Output format:
in the form of a line in a conventional format of the output product of the two numbers A + Bi, the real and imaginary part 2 decimal places. Note: If B is negative, should be written A- | B | i form.

Sample input:

2.3 3.5 5.2 0.4

Sample output:

-8.68-8.23i

My code:

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#include<sstream>
using namespace std;
//有的时候题目是一起做的,所以会有不需要的头文件

int main()
{
    double r1,r2,p1,p2;
    cin>>r1>>p1>>r2>>p2;

    double res_a=r1*r2*cos(p1+p2);
    double res_b=r1*r2*sin(p1+p2);

    bool flag_a=abs(res_a)>=0.01?true:false;//有数|为0
    bool flag_b=abs(res_b)>=0.01?true:false;

    if(!flag_a&&!flag_b)
    {
        printf("0");//0+0i
        return 0;
    }

    if(flag_a)printf("%.2f",res_a);//a+xi
    else printf("0.00");//0+xi

    if(flag_b)printf("%+.2fi",res_b);//a+bi
    else printf("+0.00i");//a+0i

    return 0;
}

Refer to the difference [C / C ++] printf () in% +. 2f and the% .2f

Published 82 original articles · won praise 1 · views 1684

Guess you like

Origin blog.csdn.net/qq_34451909/article/details/104835172