其实除了Python,Ruby外,Java也自带高精,只不过被封装在类里.Java有两个高精类:BigInteger和BigDecimal,分别是高精度整数和高精度小数.本文来简单介绍一下BigInteger类.
构造
BigInteger类只能从String构造.如,
BigInteger a = new BigInteger("233") ;
如果要用Scanner读入一个BigInteger,可以这么写:
Scanner scan = new Scanner(System.in) ;
BigInteger a = new BigInteger(scan.next()) ;
函数
因为Java不支持重载运算符,所以所有的运算都由函数来实现.
BigInteger add(BigInteger val) //求和
BigInteger subtract(BigInteger val) //相减
BigInteger multiply(BigInteger val) //求积
BigInteger divide(BigInteger val) //求商
BigInteger mod(BigInteger val) //取模
BigInteger remainder(BigInteger val) //求余
BigInteger pow(int exponent) //求幂
BigInteger abs() //绝对值
BigInteger gcd(BigInteger val) //求gcd
BigInteger max(BigInteger val) //求较大值
BigInteger min(BigInteger val) //求较小值
BigInteger negate() //相反数
BigInteger leftShift(int n) //左移
BigInteger rightShift(int n) //右移
BigInteger and(BigInteger val) //求与
BigInteger andNot(BigInteger val) //求与+取反(与非)
BigInteger or(BigInteger val) //求或
BigInteger xor(BigInteger val) //求异或
BigInteger not() //取反
int intValue() //转int
long longValue() //转long
double doubleValue() //转double
float floatValue() /转float
byte[] toByteArray(BigInteger val) //转byte数组(底层实现是由byte数组的反码构成的)
String toString() //转字符串
最后说一句,BigInteger在java.math库中.
Code
import java.util.* ;
import java.io.* ;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in) ;
BigInteger a = new BigInteger(scan.next()), b = new BigInteger(scan.next()) ;
System.out.println(a.add(b)) ;
System.out.println(a.subtract(b)) ;
System.out.println(a.multiply(b)) ;
System.out.println(a.divide(b)) ;
System.out.println(a.mod(b)) ;
}
}