PAT——1051. 复数乘法

复数可以写成(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

 1 package com.hone.basical;
 2 
 3 import java.util.Scanner;
 4 
 5 /**
 6  * 原题目:https://www.patest.cn/contests/pat-b-practise/1051
 7  * 
 8  * @author Xia 了解其规律,然后一一对运算即可。 有个小小的检测没有过,缺1分
 9  */
10 public class basicalLevel1051pluralMultipy {
11 
12     public static void main(String[] args) {
13 
14         Scanner in = new Scanner(System.in);
15         double R1 = in.nextDouble();
16         double P1 = in.nextDouble();
17         double R2 = in.nextDouble();
18         double P2 = in.nextDouble();
19         double rs, ps;
20         rs = R1 * Math.cos(P1) * R2 * Math.cos(P2) - R1 * Math.sin(P1) * R2 * Math.sin(P2);
21         ps = R1 * Math.sin(P1) * R2 * Math.cos(P2) + R2 * Math.sin(P2) * R1 * Math.cos(P1);
22         //因为牵扯到四舍五入,所有对于小于0.001,都可以当作零处理
23         if (Math.abs(rs)<=0.001) {
24             System.out.println("0.00")    ;
25         }else {
26             System.out.printf("%.2f", rs);
27         }
28         
29         if (ps > 0){
30             System.out.printf("+%.2fi", ps);
31         }else if (Math.abs(ps)<=0.001) {
32             System.out.println("+0.00i");
33         }else{
34             System.out.printf("%.2fi", ps);
35         }
36     }
37 }
原文地址:https://www.cnblogs.com/xiaxj/p/7998224.html