java大整数类(超清晰)

java刷题目遇到大整数真的不要太舒服,但我发现java的大整数类功能贼多,自己掌握的很少,所以写篇博客学习一下…

BigInteger和BigDecimal都位于java.math包中(不要和java.lang.Math搞混了!!).
api文档中是这么介绍java.math包的:

Provides classes for performing arbitrary-precision integer arithmetic (BigInteger) and arbitrary-precision decimal arithmetic (BigDecimal). BigInteger is analogous to the primitive integer types except that it provides arbitrary precision, hence operations on BigIntegers do not overflow or lose precision. In addition to standard arithmetic operations, BigInteger provides modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations. BigDecimal provides arbitrary-precision signed decimal numbers suitable for currency calculations and the like. BigDecimal gives the user complete control over rounding behavior, allowing the user to choose from a comprehensive set of eight rounding modes.
提供用于执行M任意精度整数算术(BigInteger)和任意精度十进制算术(BigDecimal)的类。 BigInteger与原始整数类型类似,不同之处在于它提供任意精度,因此对BigIntegers的操作不会溢出或丢失精度。除标准算术运算外, BigInteger还提供模块化算术,GCD计算,素数测试,素数生成,位操作和其他一些其他运算。 BigDecimal提供适用于货币计算等的任意精度的带符号十进制数。 BigDecimal为用户提供了对舍入行为的完全控制,使用户可以从八种舍入模式的全面选择中进行选择(我是不是贼贴心嘿嘿嘿)

BigInteger类提供了四个最基本的BigInteger常量,分别是

		System.out.println(BigInteger.ONE);//输出1
    	System.out.println(BigInteger.TEN);//输出10
    	System.out.println(BigInteger.TWO);//输出2
    	System.out.println(BigInteger.ZERO);//输出0
1
10
2
0

就BigInteger而言,他有八个构造函数:

  • BigInteger​(byte[] val)
    -将包含BigInteger的二进制补码二进制表示形式的字节数组转换为BigInteger。
  • BigInteger​(byte[] val, int off, int len)
    -将包含BigInteger的二进制补码二进制表示形式的字节子数组转换为BigInteger。
  • BigInteger​(int signum, byte[] magnitude)
    -将BigInteger的符号幅度表示转换为BigInteger。
  • BigInteger​(int signum, byte[] magnitude, int off, int len)
    将BigInteger的符号幅度表示转换为BigInteger
  • BigInteger​(int bitLength, int certainty, Random rnd)
    使用指定的bitLength构造一个随机生成的可能为素数的正BigInteger。
  • BigInteger​(int numBits, Random rnd)
    构造一个随机生成的BigInteger,均匀分布在0到(2^numBits -1)之间(包括0和2 )
  • BigInteger​(String val)
    将BigInteger的十进制String表示转换为BigInteger。
  • BigInteger​(String val, int radix) 将指定基数中的BigInteger的String表示转换为BigInteger。

就写题目而言常用的好像就下面俩构造函数=-=
以下是大整数相关的api

import java.util.*;
import java.math.BigInteger;
import java.math.*;
public class Main{
    
    public static void main(String[] args) {
    	//随便打的两个数,不过用生成随机大整数不是更香嘛
    	BigInteger number1=new BigInteger("347238462384523623645237465237415234165234615246742354");
    	BigInteger number2=new BigInteger("42673547263541874637462394142837645");
    	//返回一个BigInteger,其值为该BigInteger的绝对值。
    	System.out.println("abs():"+number1.abs().toString());
    	//返回值为BigInteger (this / val)。
    	System.out.println("divide():"+number1.divide(number2));
    	//返回值为的BigInteger (this % val)。
    	System.out.println("remainder():"+number1.remainder(number2));
    	//返回值为的BigInteger (this + val)。
    	System.out.println("add():"+number1.add(number2).toString());
    	//返回值为的BigInteger (this << n)。
    	System.out.println("shiftLeft():"+number1.shiftLeft(1));
    	//返回值为的BigInteger (this >> n)。
    	System.out.println("shiftRight():"+number1.shiftRight(1));
    	//返回此BigInteger的signum函数 返回-1,0,1作为BigInteger的符号
    	System.out.println("signum():"+number1.signum());
    	//返回此BigInteger的整数平方根。
    	System.out.println("sqrt():"+number1.sqrt());
    	//返回值为的BigInteger (this & val)。
    	System.out.println("and():"+number1.and(number2));
    	//返回值为的BigInteger (this & ~val)。
    	System.out.println("andNot():"+number1.andNot(number2));
    	//返回此BigInteger的二进制补码表示形式中不同于其符号位的位数.
    	//此方法在此BigInteger,从它的符号位不同的补码表示返回的比特数
    	System.out.println("bitCount():"+number1.bitCount());
    	//	返回此BigInteger的最小2补码表示形式中的位数,不包括符号位。
    	System.out.println("bitLength():"+number1.bitLength());
    	//将其转换BigInteger为byte,以检查是否丢失了信息。如果超出了,会丢出一个ArithmeticException异常
    	//System.out.println("byteValueExact():"+number1.byteValueExact());
    	//返回一个BigInteger,其值等于该BigInteger,并且清除了指定的位。
    	System.out.println("clearBit:"+number2.clearBit(0));
    	//将此BigInteger与指定的BigInteger进行比较。
    	System.out.println("compareTo():"+number1.compareTo(number2));
    	
    	//返回两个BigIntegers组成的数组,其中包含(this / val) 后跟(this % val)。
    	System.out.println("divideAndRemainder():"+Arrays.toString(number1.divideAndRemainder(number2)));
    	//将此BigInteger转换为double。
    	System.out.println("doubleValue():"+number1.doubleValue());
    	//将此BigInteger转换为float。
    	System.out.println("floatValue():"+number1.floatValue());
//    	返回此BigInteger和的最大值val。
    	System.out.println("max():"+number1.max(number2));
    	//返回此BigInteger和的最小值val。
    	System.out.println("min():"+number1.min(number2));
    	//返回值为的BigInteger (this mod m)。
    	System.out.println("mod():"+number1.mod(number2));
    	//返回值为(this-1 的BigInteger mod m)。
    	System.out.println("modInverse():"+number1.modInverse(number2));
    	//返回值为的BigInteger 。(thisexponent mod m)
    	System.out.println("modPow():"+number1.modPow(BigInteger.valueOf(1), number2));
    	//返回值为的BigInteger (this * val)。
    	System.out.println("multiply():"+number1.multiply(number2));
    	//返回值为的BigInteger (-this)。 
    	System.out.println("negate():"+number1.negate());
    	//将此BigInteger与指定的Object比较是否相等。
    	System.out.println("equals()就不说了,大家都明白....");
    	//返回一个BigInteger,其值等于该BigInteger的指定位被翻转。
    	System.out.println("flipBit():"+number1.flipBit(1));  	
    	//返回一个BigInteger,其值是abs(this)和的最大公约数 abs(val)。
    	System.out.println("gcd():"+number1.gcd(number2));
    	//返回此BigInteger中最右边(最低位)的一位的索引(最右边一位的右边的零位数)。
    	System.out.println("getLowestSetBit():"+number1.getLowestSetBit());
    	//返回此BigInteger的哈希码。
    	System.out.println("hashCode():"+number1.hashCode());
    	//将此BigInteger转换为int。
    	System.out.println("intValue():"+number1.intValue());
    	//	将此转换BigInteger为int,以检查是否丢失了信息。
    	//System.out.println("intValueExact():"+number1.intValueExact());
    	//true如果此BigInteger可能是质数,false则返回, 如果它绝对是复合的。
    	System.out.println("isProbablePrime():"+number1.isProbablePrime(10));
    	//将此BigInteger转换为long。
    	System.out.println("longValue():"+number1.longValue());
    	//将其转换BigInteger为long,以检查是否丢失了信息。
    	//System.out.println("longValueExact()"+number1.longValueExact());
    	//将其转换BigInteger为short,以检查是否丢失了信息。
    	//System.out.println("shortValueExact"+number1.shortValueExact());
    	
    	//返回大于此BigInteger可能是质数的第一个整数。
    	System.out.println("nextProbablePrime():"+number1.nextProbablePrime());
    	//返回值为的BigInteger (~this)。
    	System.out.println("not():"+number1.not());
    	//返回值为的BigInteger (this | val)。
    	System.out.println("or():"+number1.or(number2));
    	//返回值为的BigInteger 。(thisexponent)
    	System.out.println("pow():"+number1.pow(2));
    	//返回带有指定bitLength的正BigInteger(可能是素数)。
    	System.out.println("probablePrime():"+number1.probablePrime(10, new Random(10)));

    

    	//返回一个BigInteger,其值与此指定位设置的BigInteger等效。
    	System.out.println("setBit():"+number1.setBit(5));
    	
    	//分别返回包含整数平方根两个BigInteger的平方根 s的this和它的其余部分this - s*s。
    	BigInteger[] arr1=number1.sqrtAndRemainder();
    	System.out.println(arr1[0]+" "+arr1[1]);
    	//返回值为的BigInteger (this - val)。    	
    	System.out.println("subtract():"+number1.subtract(number2));
    	//	true仅当设置了指定位时返回。
    	System.out.println("testBit():"+number1.testBit(1));
    	//	返回一个字节数组,其中包含此BigInteger的二进制补码表示形式
    	System.out.println("toByteArray():"+Arrays.toString(number1.toByteArray()));
    	//返回此BigInteger的十进制String表示形式。
    	System.out.println("toString()不多说了....");
    	//以给定的基数返回此BigInteger的String表示形式。
    	//返回此BigInteger在给定的基数的字符串表示形式。如果基数是从Character.MIN_RADIX到Character.MAX_RADIX包容的范围内,它会默认为10(因为Integer.toString的情况下)。注释链接:
    	https://www.yiibai.com/java/math/biginteger_tostring_radix.html
		//说白了就是修改进制...
    	System.out.println("toString(int radix):"+number1.toString(10));
    	//返回一个BigInteger,其值等于指定的long。
    	System.out.println("valueOf():"+BigInteger.valueOf(8));
    	//返回值为的BigInteger (this ^ val)
    	System.out.println("xor(BigInteger val):"+number1.xor(number2));
    	
    }
}

以下是结果

abs():347238462384523623645237465237415234165234615246742354
divide():8137089242665013774
remainder():534213018473473171115567876020124
add():347238462384523623687911012500957108802697009389579999
shiftLeft():694476924769047247290474930474830468330469230493484708
shiftRight():173619231192261811822618732618707617082617307623371177
signum():1
sqrt():589269431062331302776634508
and():42188789727018586573424610810872576
andNot():347238462384523623603048675510396647591810004435869778
bitCount():82
bitLength():178
clearBit:42673547263541874637462394142837644
compareTo():1
divideAndRemainder():[8137089242665013774, 534213018473473171115567876020124]
doubleValue():3.4723846238452363E53
floatValue():Infinity
max():347238462384523623645237465237415234165234615246742354
min():42673547263541874637462394142837645
mod():534213018473473171115567876020124
modInverse():17920544566730946678389678251539389
modPow():534213018473473171115567876020124
multiply():14817896936285576249282678241822399816218930984900061135427897710474330996361247767116330
negate():-347238462384523623645237465237415234165234615246742354
equals()就不说了,大家都明白....
flipBit():347238462384523623645237465237415234165234615246742352
gcd():1
getLowestSetBit():1
hashCode():-79126074
intValue():1848206162
isProbablePrime():false
longValue():4432267828819223378
nextProbablePrime():347238462384523623645237465237415234165234615246742411
not():-347238462384523623645237465237415234165234615246742355
or():347238462384523623645722222773938522229272398578707423
pow():120574549759168227502336599248901286606998151874104553572496655773107041720898305500980652795649209257461316
probablePrime():761
setBit():347238462384523623645237465237415234165234615246742386
589269431062331302776634508 683332853438534624230340290
subtract():347238462384523623602563917973873359527772221103904709
testBit():true
toByteArray():[3, -96, 22, 50, -125, 1, -106, 62, 10, -24, 25, 111, -23, -33, -88, 61, -126, -108, 27, 110, 41, 99, 82]
toString()不多说了....
toString(int radix):164005431203003130760256403133764737520366024501555612261522
valueOf():10
xor(BigInteger val):347238462384523623603533433046919935655847787767834847

突然有个神奇的想法,通过java的反射机制我不是能少打很多代码嘛…
另外需要大整数的题目对于Scanner一般都不友好,所以可能需要修改一下…

		//一个小小的模板,贼好使..
 		BufferedReader inScanner = new BufferedReader(new InputStreamReader(System.in));    
		String[] liStrings=inScanner.readLine().split(" ");

希望自己越来越棒!!!

你可能感兴趣的:(java,编程,pat乙级,java,算法)