Java中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类。
这两个类都在java.math.*包中,因此每次必须在开头处引用该包。
基本常量
BigInteger.ONE 1
BigInteger.TEN 10
BigInteger.ZERO 0
BigInteger转为整型(浮点型类似):
BigInteger x;
int s=x.intValue();
long ss=x.longValue();
常用函数:
valueOf(long x); 将x转换为BigInteger并返回
add(); 相加
subtract(); 相减
multiply(); 相乘
divide(); 相除取整
remainder(); mod(); 取余
pow(); 次方
gcd(); 最大公约
abs(); 绝对值
max(); min(); 最大最小
compareTo(); 返回-1,0,1 小于、等于、大于
equals(); 相等
构造函数:
BigInteger(String) 将字符串以十进制方式存储为大数
BigInteger(String val,int radix); 字符串val的基数为radix,例如val="11",radix=2,则大数的十进制为3
不常用函数:
and(); or(); not(); 与、或、取反
negate(); 取相反数
toString(int radix); 返回对象的radix进制的字符串
BigInteger[] divideAndRemainder(BigInteger val) 用数组返回商和余数
boolean isProbablePrime(BigInteger n) 判断大数是否为素数
nextProbablePrime(BigInteger n) 返回比大数n大的为素数的数
modPow(BigInteger n, BigInteger mod) 计算this^n % mod
shiftLeft(int n) 返回其值为 (this << n) 的 BigInteger
shiftRight(int n) 返回其值为 (this >> n) 的 BigInteger
输入输出
nextBigInteger(radix); radix进制形式输入
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
BigInteger x = cin.nextBigInteger();
x = x.add(BigInteger.valueOf(10)).subtract(new BigInteger("5"));//x=x+10-5
System.out.println(x);
System.out.println(x.toString());
}
}
}