当前位置: 代码迷 >> 综合 >> Java BigInteger 的基本用法
  详细解决方案

Java BigInteger 的基本用法

热度:29   发布时间:2024-02-01 06:41:11.0

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()){ //等同于!=EOFString 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 < ba.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)   // ^^^^^^^^^^
  相关解决方案