蓝桥杯的一部分填空题的数字需要使用BigInteger来表示而且这个数字特别大
在控制台上不能输出时,这时我们可以输出到文件中,
以2018年的省赛真题复数幂为例讲解 通过测试的哈,这个代码是一定满分的
单独的输出讲解在最后面
package readlBlue2018;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigInteger;
/**
* 复数幂
* @author 楠
*
*/
public class Demo03 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BigInteger a=new BigInteger("2");
BigInteger b=new BigInteger("3");
int c=123456;
method(a, b, c);
BigInteger temp=new BigInteger("0");
System.out.print(shi);
FileWriter fileWriter=new FileWriter("D:\\123.txt");
fileWriter.write(shi.toString());
fileWriter.write("\n");
fileWriter.write(xu.toString());
fileWriter.flush();
fileWriter.close();
// if(xu.compareTo(temp)==1) {//虚部是正数
// System.out.print("+"+xu+"i");
// }else {//虚部是负数
// System.out.print(xu+"i");
// }
}
static BigInteger shi=new BigInteger("1");
static BigInteger xu=new BigInteger("1");
public static void method(BigInteger a,BigInteger b,int c) {
if(c<1) {
return;
}
if(c==1) {
shi=a;
xu=b;
return;
}
if(c%2==0) {//偶数
BigInteger temp=new BigInteger("2");
shi=a.multiply(a).subtract(b.multiply(b));
xu=a.multiply(b).multiply(temp);
method(shi, xu, c/2);
}else {
method(a, b, c-1);
BigInteger temp=shi;
shi=shi.multiply(a).subtract(xu.multiply(b));
xu=xu.multiply(a).add(b.multiply(temp));
}
}
}