当前位置:网站首页>1051 Multiplication of Complex Numbers (15 points)

1051 Multiplication of Complex Numbers (15 points)

2022-08-11 07:47:00 dumb bit

题目要求:
在这里插入图片描述
代码:

import java.util.Scanner;

public class Main {
    
    public static void main(String[] args) {
    
        //輸入
        Scanner scanner = new Scanner(System.in);

        double r1 = scanner.nextDouble();
        double p1 = scanner.nextDouble();
        double r2 = scanner.nextDouble();
        double p2 = scanner.nextDouble();

        double a = r1 * Math.cos(p1);
        double b = r1 * Math.sin(p1);
        double c = r2 * Math.cos(p2);
        double d = r2 * Math.sin(p2);
        //(a+bi)*(c+di) = ac + adi +bci -bd = (ac-bd) + (ad+bc)i

        double s = a * c - b * d;
        String sb = "-0.00".equals(String.format("%.2f",s)) ? "0.00" : String.format("%.2f",s);
        double x = a * d + b * c;
        String xb =  x >= 0 ? (String.format("+%.2fi", x)) : (String.format("%.2fi", x));
        if ("-0.00i".equals(xb)) {
    
            xb = "+0.00i";
        }
        System.out.println(sb + xb);

    }
}

结果:
在这里插入图片描述

原网站

版权声明
本文为[dumb bit]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110645413899.html