【蓝桥杯】2018.B组.Java.第三题

复数幂

设i为虚数单位。对于任意正整数n,(2+3i)^n 的实部和虚部都是整数。
求 (2+3i)^123456 等于多少? 即(2+3i)的123456次幂,这个数字很大,要求精确表示。
答案写成 “实部±虚部i” 的形式,实部和虚部都是整数(不能用科学计数法表示),中间任何地方都不加空格,实部为正时前面不加正号。(2+3i)^2 写成: -5+12i,
(2+3i)^5 的写成: 122-597i

题解

在这里插入图片描述

import java.math.BigInteger;

public class Main {
    public static void main(String[] args) {
        BigInteger a = new BigInteger("2");
        BigInteger A =a;
        BigInteger b = new BigInteger("3");
        BigInteger B= b;

        for (int i = 1; i < 123456; i++) {
            BigInteger t =  A.multiply(a).subtract(B.multiply(b));
            B = A.multiply(b).add(B.multiply(a));
            A = t;
        }
        if (B.compareTo(BigInteger.ZERO) < 0) {
            System.out.println(A.toString()+B.toString()+"i");
        }else
            System.out.println(A.toString()+"+"+B.toString()+"i");

    }
}

猜你喜欢

转载自blog.csdn.net/HuaLingPiaoXue/article/details/83867183
今日推荐