BigInteger:
BigInteger和BigDecimal分別表示大整數(shù)類和大浮點(diǎn)數(shù)類写穴,至于兩個類的對象能表示最大范圍不清楚
理論上能夠表示無線大的數(shù),只要計算機(jī)內(nèi)存足夠大雌贱。
基本函數(shù):
- valueOf(parament); 將參數(shù)轉(zhuǎn)換為制定的類型
String s=”12345”;
BigInteger c=BigInteger.valueOf(s);
則c=12345啊送;
- add(); 大整數(shù)相加
BigInteger a=new BigInteger(“23”);
BigInteger b=new BigInteger(“34”);
a. add(b);
subtract(); 相減
multiply(); 相乘
divide(); 相除取整
remainder(); 取余
pow(); a.pow(b)=a^b
gcd(); 最大公約數(shù)
abs(); 絕對值
negate(); 取反數(shù)
mod(); a.mod(b)=a%b=a.remainder(b);
max(); min();
punlic int comareTo();
boolean equals(); 是否相等
BigInteger構(gòu)造函數(shù):
一般用到以下兩種:
BigInteger(String val);
將指定字符串轉(zhuǎn)換為十進(jìn)制表示形式;
BigInteger(String val,int radix);
將指定基數(shù)的 BigInteger 的字符串表示形式轉(zhuǎn)換為 BigInteger
Ⅱ.基本常量:
A=BigInteger.ONE 1
B=BigInteger.TEN 10
C=BigInteger.ZERO 0
Ⅲ.基本操作
- 讀入:
用Scanner類定義對象進(jìn)行控制臺讀入,Scanner類在java.util.*包中
Scanner cin=new Scanner(System.in);// 讀入
while(cin.hasNext()) //等同于!=EOF
{
int n;
BigInteger m;
n=cin.nextInt(); //讀入一個int;
m=cin.BigInteger();//讀入一個BigInteger;
System.out.print(m.toString());
}
if( a.compareTo(b) == 0 ) System.out.println("a == b"); //大整數(shù)a==b
else if( a.compareTo(b) > 0 ) System.out.println("a > b"); //大整數(shù)a>b
else if( a.compareTo(b) < 0 ) System.out.println("a < b"); //大整數(shù)a<b
//大整數(shù)絕對值
System.out.println(a.abs()); //大整數(shù)a的絕對值
//大整數(shù)的冪
int exponent=10;
System.out.println(a.pow(exponent)); //大整數(shù)a的exponent次冪
//返回大整數(shù)十進(jìn)制的字符串表示
System.out.println(a.toString());
//返回大整數(shù)p進(jìn)制的字符串表示
int p=8;
System.out.println(a.toString(p));