Java BigInteger 的基本用法

BigInteger 类在 java.math.BigInteger 包中

import java.math.BigInteger;	//引用包
对象创建
BigInteger a = new BigInteger("123"); // 这里是字符串

String str = "111";
BigInteger c = new BigInteger(String.valueOf((str)));

int cc = 22;        
BigInteger ccc = BigInteger.valueOf(cc);

a = BigInteger.ONE // 1		基本常量
b = BigInteger.TEN // 10
c = BigInteger.ZERO // 0
输入输出
Scanner in = new Scanner(System.in); 
//直接读入 BigInteger
while(in.hasNext()){ //等同于!=EOF 无限读入
    BigInteger a;
    a = in.nextBigInteger();
    System.out.print(a.toString());
}

//间接读入 BigInteger
while(in.hasNext()){ //等同于!=EOF
    String s = in.nextLine();
    BigInteger a = new BigInteger(s);
    System.out.print(a.toString());
}
基本运算
if(a.compareTo(b) == 0) System.out.println("a == b"); // a == b
else if(a.compareTo(b) > 0) System.out.println("a > b"); // a > b
else if(a.compareTo(b) < 0) System.out.println("a < b"); // a < b

a.add(b); //  +++++++++
a.subtract(b);  //  -----------
a.multiply(b);  //  ***********
a.divide(b);	//  ///////////
BigInteger c = b.remainder(a); //  %%%%%%%%%%
System.out.println(a.gcd(b));  //  gcd
a.abs()  //  abs
a.negate()  //  相反数
a.pow(3)   // ^^^^^^^^^^

你可能感兴趣的:(大数模板)