# PAT (Basic Level) Practice (Chinese) 1051 complex multiplication (15 minutes) (Java implementation)

Posted by klevis miho on Wed, 23 Oct 2019 21:30:41 +0200

# Title Description:

The complex number can be written as the general form of (A+Bi)(A+Bi)(A+Bi) (a + bi) (a + bi), in which AAA is the real part, BBB is the virtual part, I I I is the unit of imaginary number, satisfying I2 = − 1i^2 = − 1i2 = − 1i2 = − 1i2 = − 1i2 = − 1i2 = − 1; or the exponential form under polar coordinates (R × e (Pi) (R × e ^ {(Pi)} (Pi)) (R × e (Pi)), in which R is the complex modulus, P is the radiation angle, I is the unit of imaginary number, its equivalence, its equivalence, its equivalent, P is the radiation angle, I is the unit of imaginary number, its equivalent, its equivalence, its equivalent, its imaginary number unit in trigonometry Form (R(cos(P)+isin(P))(R(cos(P)+isin(P))(R(cos(P)+isin(P)).

Given the R and P of two complex numbers, it is required to output the normal form of the product of two numbers.

# Input format:

Input two complex numbers R1,P1,R2,P2 1, p 1, R 2, P 2 R1,P1,R2,P2 in a row, separated by spaces.

# Output format:

In one line, the normal form of the product of two numbers is output according to the format of A+Bi. Both the real part and the virtual part retain two decimal places. Note: if B is a negative number, it should be written as A-|B|i.

# Input example:

```2.3 3.5 5.2 0.4
```

# Output example:

```-8.68-8.23i
```

# Code example (Java implementation)

```import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner cin = new Scanner(System.in);

double a0 = cin.nextDouble();
double b0 = cin.nextDouble();
double c0 = cin.nextDouble();
double d0 = cin.nextDouble();

double a = a0 * Math.cos(b0);
double b = a0 * Math.sin(b0);
double c = c0 * Math.cos(d0);
double d = c0 * Math.sin(d0);

double i = (a * c - b * d);
double j = (a * d + b * c);

/**
* There is a little pit when processing the calculation results.
* For real part: if the result after decimal point processing is: - 0.00, it needs to be converted to 0.00.
* For the imaginary part: if the result after the decimal point is: - 0.00i, it needs to be converted to + 0.00i.
* In fact, I have some doubts about this transformation, because in fact, the sign of the calculation result itself is a negative number.
* It's smaller than 0, and when it turns out like this, it's not a negative number.
* But if we only see five in one, we can understand it again.
* Hee hee
*/
String result = String.format("%.2f", i);
if ("-0.00".equals(result)) {
result = "0.00";
}

String result1 =  j >= 0 ? (String.format("+%.2fi", j)) : (String.format("%.2fi", j));
if ("-0.00i".equals(result1)) {
result1 = "+0.00i";
}
System.out.println(result + result1);
}

}
```

Topics: Java