1. 程式人生 > >java大整數模版

java大整數模版

importjava.math.BigInteger;   publicclassabc   {       publicstaticvoidmain(String[] args)       {           BigInteger bigInteger1=newBigInteger("123456789012345689123413241234109999321413253426256");           BigInteger bigInteger2=newBigInteger("9999999999999999999999999999999999999999999999999");           //add(加)           bigInteger1=bigInteger1.add(bigInteger2);           System.out.println(bigInteger1);           //subtract(減)           bigInteger1=bigInteger1.subtract(bigInteger2);           System.out.println(bigInteger1);           //multiplay(乘)           bigInteger1=bigInteger1.multiply(bigInteger2);           System.out.println(bigInteger1);           //divide(除)           bigInteger1=bigInteger1.divide(bigInteger2);           System.out.println(bigInteger1);           //negate(取相反數)           bigInteger1=bigInteger1.negate();           System.out.println(bigInteger1);           //pow(次方)           bigInteger1=bigInteger1.pow(10);           System.out.println(bigInteger1);           //最後一個(pow)輸出822526259969629520153101122399497267810820646209100034107289685263315159632308144831374076434409138022661540729292142535548443646527802945487955787074321901682016392981171243389906853091274410210521602192777845202346331669556282743561193277694935461774695127206357303815843367925743175834034307102019549300834630106026193909111800769211331213033566417046739040269952221223183087733472654826824099458751406700255831111833751227821269483039466115059377165177024376865993794139752199224597958945119666176       }   }   BigInteger支援任意精度的整數。也就是說,在運算中,可以準確地表示任何大小的整數值,而不會丟失任何資訊。   常用方法:   abs()//返回其值是此BigInteger的絕對值的BigInteger。   add(BigInteger val)//返回其值為(this+val)的BigInteger。   subtract(BigIntegerval)//返回其值為(this-val)的BigInteger。   multiply(BigInteger val) //返回其值為(this*val)的BigInteger。   divide(BigInteger val)//返回其值為(this/val)的BigInteger。   remainder(BigInteger val)//返回其值為(this%val)的BigInteger。   compareTo(BigInteger val)//將此BigInteger與指定的BigInteger進行比較。返回值1、0、-1分別表示大於、等於、小於   pow(intexponent) //返回當前大數的exponent次冪。   toString()//返回此BigInteger的十進位制字串表示形式。   toString(intradix)//返回此BigInteger的給定基數(radix進位制)的字串表示形式。       /*BigInteger和BigDecimal是在java.math包中已有的類,前者表示整數,後者表示浮點數。        為什麼用大數字?        1)BigInteger:支援任意精度的整數,可以精確地表示任意大小的整數值,同時在運算過程中不會丟失任何資訊。        2)BigInteger:可以精確地表示任意精度的小數,同時在運算過程中不會丟失任何資訊。        注意:不能直接用符號如+、-來使用大數字,例如:*/       importjava.math.BigInteger;       publicclassabc {           publicstaticvoidmain(String[] args) {               inta = 231, b = 423, c = 1393;               BigInteger x, y, z, ans;               x = BigInteger.valueOf(a);               y = BigInteger.valueOf(b);               z = BigInteger.valueOf(c);               ans = x.add(y); //加運算               System.out.print(ans+"");               ans = z.divide(y); //除運算               System.out.print(ans+"");               ans = x.mod(z); //模運算               System.out.print(ans+"");               if(ans.compareTo(x) == 0)                   System.out.println("1");           }       }   //運算結果:6543 231 1   /*主要有以下方法可以使用:    BigIntegeradd(BigInteger other)    BigIntegersubtract(BigInteger other)    BigIntegermultiply(BigInteger other)    BigIntegerdivide(BigInteger other)    BigIntegermod(BigIntegerother)    intcompareTo(BigInteger other)    staticBigInteger valueOf(long x)*/   ---------------------  作者:chaojidage  來源:CSDN  原文:https://blog.csdn.net/zsd201531107026/article/details/78617684  版權宣告:本文為博主原創文章,轉載請附上博文連結!