ACM大数-省时省力-JAVA BigInteger类
打比赛时候经常遇到运用大数据题目,又懒得敲那么长的代码,Java中有一个类就是用来处理很大的数据的类,为BigInteger类。
哈哈 省时省力
下面是BigInteger类常用的方法,基本上所有的操作要转换成BigInteger对象来进行处理
具体的测试代码如下:
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
//加法
BigInteger add1 = new BigInteger("10");
System.out.println(add1.add(new BigInteger("20")));
//减法
BigInteger sub1 = new BigInteger("10");
System.out.println(sub1.subtract(new BigInteger("20")));
//除法
BigInteger div1 = new BigInteger("10");
System.out.println(div1.divide(new BigInteger("20")));
//乘法
BigInteger mul1 = new BigInteger("10");
System.out.println(mul1.multiply(new BigInteger("20")));
//取模
BigInteger remain1 = new BigInteger("10");
System.out.println(remain1.remainder(new BigInteger("8")));
//快速幂取模
BigInteger mod = new BigInteger ("10");
BigInteger pow = new BigInteger ("20");
System.out.println(pow.modPow(pow,mod));
//比较两个数字的大小,小于返回-1,大于返回1,等于0的时候返回0
BigInteger comp1 = new BigInteger("10");
System.out.println(comp1.compareTo(new BigInteger("18")));
//n次幂的运算
BigInteger power1 = new BigInteger("2");
System.out.println(power1.pow(10));
//返回较小的数
BigInteger min1 = new BigInteger("2");
System.out.println(min1.min(new BigInteger("-23")));
//返回较大的数
BigInteger max1 = new BigInteger("2");
System.out.println(max1.max(new BigInteger("-23")));
//返回该类型的数字的值
BigInteger val = new BigInteger("123");
System.out.println(val.intValue());
//返回最大公约数
BigInteger gcd1 = new BigInteger("12");
System.out.println(gcd1.gcd(new BigInteger("6")));
//取反
BigInteger neg1 = new BigInteger("12");
System.out.println(neg1.negate());
//按位与
BigInteger and1 = new BigInteger("10");
System.out.println(and1.and(new BigInteger("1")));
//按位或
BigInteger or1 = new BigInteger("10");
System.out.println(or1.or(new BigInteger("10")));
//异或
BigInteger xor1 = new BigInteger("10");
System.out.println(xor1.xor(new BigInteger("10")));
//返回n进制的字符串(进制的转换)
BigInteger decimal1 = new BigInteger("12");
System.out.println(decimal1.toString(2));
//返回数字的绝对值
BigInteger abs1 = new BigInteger("-12");
System.out.println(abs1.abs());
//检测某位上是否为1
BigInteger testBit1 = new BigInteger("4");
System.out.println(testBit1.testBit(2));
//左移1位
BigInteger moveLeftBit1 = new BigInteger("4");
System.out.println(moveLeftBit1.shiftLeft(1));
//右移1位
BigInteger moveRightBit1 = new BigInteger("4");
System.out.println(moveRightBit1.shiftLeft(-1));
//非
BigInteger not = new BigInteger ("10");
System.out.println(not.not());
//valueOf()初始化
//返回当前大整数的相反数
BigInteger negate = new BigInteger ("10");
System.out.println(negate.negate());
//判断质数,并返回
BigInteger prime = new BigInteger ("10");
System.out.println(prime.probablePrime());
System.out.println(prime.nextprobablePrime());
}
}