MENU

第九届蓝桥杯 Java A—— 复数幂

March 22, 2019 • Read: 3499 • 算法阅读设置

设 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) {
  • Number res = new Number();
  • Number tmp = new Number();
  • for (int i = 1; i < 5; i++) {
  • BigInteger al = res.a.multiply(tmp.a);
  • BigInteger ar = res.b.multiply(tmp.b);
  • BigInteger bl = res.a.multiply(tmp.b);
  • BigInteger br = res.b.multiply(tmp.a);
  • res.a = al.subtract(ar);
  • res.b = bl.add(br);
  • }
  • System.out.println(res.a + "" + res.b + "i");
  • }
  • }
  • class Number {
  • BigInteger a = new BigInteger("2");
  • BigInteger b = new BigInteger("3");
  • }
Archives Tip
QR Code for this page
Tipping QR Code