【藍橋杯】2018.B組.Java.第三題
阿新 • • 發佈:2018-12-23
複數冪
設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");
}
}