ACM与Java -- 大整数类的常用函数一览表
| BigInteger abs()? 此方法返回一個BigInteger,其值是此BigInteger的絕對值。 | |
| 2 | BigInteger add(BigInteger val) 此方法返回一個BigInteger,其值是(this + val). |
| 3 | BigInteger and(BigInteger val) 此方法返回一個BigInteger,其值是 (this & val). |
| 4 | BigInteger andNot(BigInteger val)? 此方法返回一個BigInteger,其值是 (this & ~val). |
| 5 | int bitCount() 此方法返回此BigInteger的二進制補碼表示的位,從符號位不同的數字。 |
| 6 | int bitLength()? 此方法返回位在此BigInteger的最小的二進制補碼表示的數,不包括符號位。 |
| 7 | BigInteger clearBit(int n) 此方法返回一個BigInteger,其值相當于此BigInteger與指定位清零。 |
| 8 | int compareTo(BigInteger val) 此方法比較此BigInteger與指定的BigInteger。 |
| 9 | BigInteger divide(BigInteger val) 此方法返回一個BigInteger,其值是 (this / val). |
| 10 | BigInteger[ ] divideAndRemainder(BigInteger val) 此方法返回一個包含兩個BigIntegers:(this / val) 和?(this % val),其次是一個數組。 |
| 11 | double doubleValue() 此方法此BigInteger轉換為雙精度double。? |
| 12 | boolean equals(Object x) 此方法比較此BigInteger與指定對象是否相等。 |
| 13 | BigInteger flipBit(int n) 此方法返回一個BigInteger,其值相當于此BigInteger與指定位翻轉。 |
| 14 | float floatValue() 此方法將BigInteger轉換為float。 |
| 15 | BigInteger gcd(BigInteger val)? 此方法返回一個BigInteger,其值是絕對值的最大公約數:abs(this)?和abs(val)。 |
| 16 | int getLowestSetBit() 此方法返回最右邊的(最低階)的索引在此BigInteger1比特(零比特的數量,以最右側的1位的右側)。 |
| 17 | int hashCode() 此方法返回此BigInteger的哈希代碼。 |
| 18 | int intValue() 此方法此BigInteger轉換為int。 |
| 19 | boolean isProbablePrime(int certainty) 此方法返回true,如果此BigInteger是素數,其絕對復合數則返回false。 |
| 20 | long longValue() 些方法將BigInteger轉換為long。 |
| 21 | BigInteger max(BigInteger val) 此方法返回此BigInteger和val的最大值。 |
| 22 | BigInteger min(BigInteger val) 此方法返回此BigInteger和val的最小值。 |
| 23 | BigInteger mod(BigInteger m) 此方法返回一個BigInteger,其值是(this mod m). |
| 24 | BigInteger modInverse(BigInteger m) 此方法返回一個BigInteger,其值是 (this-1?mod m). |
| 25 | BigInteger modPow(BigInteger exponent, BigInteger m) 此方法返回一個BigInteger,其值是 (thisexponent?mod m). |
| 26 | BigInteger multiply(BigInteger val) 此方法返回一個BigInteger,其值是 (this * val). |
| 27 | BigInteger negate() 此方法返回一個BigInteger,其值是 (-this). |
| 28 | BigInteger nextProbablePrime()? 此方法返回一個整數大于該BigInteger的可能是素數。 |
| 29 | BigInteger not() 此方法返回一個BigInteger,其值是 (~this). |
| 30 | BigInteger or(BigInteger val) 此方法返回一個BigInteger,其值是 (this | val). |
| 31 | BigInteger pow(int exponent) 此方法返回一個BigInteger,其值是(thisexponent). |
| 32 | static BigInteger probablePrime(int bitLength, Random rnd) 此方法返回一個正BigInteger的可能是素數,以指定的bitLength。 |
| 33 | BigInteger remainder(BigInteger val) 此方法返回一個BigInteger,其值是 (this % val). |
| 34 | BigInteger setBit(int n) 此方法返回一個BigInteger,其值相當于此BigInteger與指定的位設置。 |
| 35 | BigInteger shiftLeft(int n) 此方法返回一個BigInteger,其值是 (this << n). |
| 36 | BigInteger shiftRight(int n) 此方法返回一個BigInteger,其值是 (this >> n). |
| 37 | int signum()? This method returns the signum function of this BigInteger. |
| 38 | BigInteger subtract(BigInteger val) 此方法返回一個BigInteger,其值是 (this - val). |
| 39 | boolean testBit(int n) 此方法返回當且僅當所指定的位被設置為真。 |
| 40 | byte[ ] toByteArray() 此方法返回一個包含此BigInteger的二進制補碼表示的字節數組。 |
| 41 | String toString()? 此方法返回此BigInteger的十進制字符串表示形式。 |
| 42 | String toString(int radix)? 此方法返回在給定的基數以BigInteger的字符串表示形式。 |
| 43 | static BigInteger valueOf(long val) 此方法返回一個BigInteger,其值等于指定long。 |
| 44 | BigInteger xor(BigInteger val)? 此方法返回一個BigInteger,其值是 (this ^ val). |
詳情https://www.yiibai.com/java/math/java_math_bigdecimal.html
大整數類:?
import java.math.BigDecimal; import java.math.BigInteger; import java.util.Scanner;public class q {public static void main(String[] args) {BigInteger a = BigInteger.valueOf(20);BigInteger b = BigInteger.valueOf(450);System.out.println(a);System.out.println(b);//加法System.out.println(a.add(b));System.out.println(a.add(a));System.out.println(a.add(b).add(b));//減法System.out.println(a.subtract(a));System.out.println(a.subtract(b));System.out.println(b.subtract(a).subtract(a));//乘法System.out.println(a.multiply(a));System.out.println(a.multiply(b));System.out.println(a.multiply(b).multiply(b));//除法System.out.println(a.divide(a));System.out.println(a.divide(b));//10/2000=0System.out.println(b.divide(a));System.out.println(b.divide(a).divide(a));//取模BigInteger mod = BigInteger.valueOf(3);System.out.println(b.mod(mod));} }大數:
構造器描述?
BigDecimal(int) ? ? ? 創建一個具有參數所指定整數值的對象。?
BigDecimal(double) 創建一個具有參數所指定雙精度值的對象。?
BigDecimal(long) ? ?創建一個具有參數所指定長整數值的對象。?
BigDecimal(String) 創建一個具有參數所指定以字符串表示的數值的對象。
方法描述?
add(BigDecimal) ? ? ? ?BigDecimal對象中的值相加,然后返回這個對象。?
subtract(BigDecimal) BigDecimal對象中的值相減,然后返回這個對象。?
multiply(BigDecimal) ?BigDecimal對象中的值相乘,然后返回這個對象。?
divide(BigDecimal) ? ? BigDecimal對象中的值相除,然后返回這個對象。?
toString() ? ? ? ? ? ? ? ?將BigDecimal對象的數值轉換成字符串。? (一般都用toPlainString())
doubleValue() ? ? ? ? ?將BigDecimal對象中的值以雙精度數返回。?
floatValue() ? ? ? ? ? ? 將BigDecimal對象中的值以單精度數返回。?
longValue() ? ? ? ? ? ? 將BigDecimal對象中的值以長整數返回。?
intValue() ? ? ? ? ? ? ? 將BigDecimal對象中的值以整數返回。?
常用搭配:stripTrailingZeros().toPlainString(); 在保證數值不變的前提下,去除后綴0,并且保留十進制的特性。 import java.math.BigDecimal; import java.math.BigInteger; import java.util.Scanner;public class q {public static void main(String[] args) {//如果加引號了或者是直接控制臺讀入的,默認使用了string的構造器,否則相當于是intBigDecimal a = new BigDecimal(10);BigDecimal b = new BigDecimal(10.5);BigDecimal c = new BigDecimal(123456789101222L);BigDecimal d = new BigDecimal("-123456.31");System.out.println(a);System.out.println(b);System.out.println(c);System.out.println(d);//四則運算System.out.println(a.add(b).add(c).add(d));System.out.println(a.subtract(b).subtract(a));System.out.println(a.multiply(b).multiply(c));System.out.println(c.divide(a).divide(new BigDecimal(20)));System.out.println(d.divide(a));System.out.println(d.doubleValue());System.out.println(a.compareTo(a));// 0System.out.println(a.compareTo(b));// -1System.out.println(b.compareTo(a));// 1BigDecimal aa = new BigDecimal(10.00);BigDecimal bb = new BigDecimal(10.000);BigDecimal cc = new BigDecimal(200);String aa = di.stripTrailingZeros().toPlainString();System.out.println(aa.equals(aa));// true?System.out.println(aa.equals(bb));// true?System.out.println(aa.equals(cc));//false}}保留位數:
例如:1.將f1保留2位有效數字輸出2.將f1保留4位有效數字輸出1.System.out.println((double)(Math.round(f1*100)/100.0));2.System.out.println((double)(Math.round(sd3*100)/100.0));注意:若數據只有小數點后第n位為0,則保留到不為0的位;如3.14003保留1位結果:3.1保留2位結果:3.14保留3位結果:3.14保留4位結果:3.14保留5位結果:3.1400 例如:1.將f1保留2位有效數字輸出 2.將f1保留4位有效數字輸出 1.System.out.println((double) (Math.round(f1*100)/100.0)); 2.System.out.println((double) (Math.round(sd3*100)/100.0)); 注意:若數據只有小數點后第n位為0,則保留到不為0的位; 如 3.14003 保留1位結果:3.1 保留2位結果:3.14 保留3位結果:3.14 保留4位結果:3.14 保留5位結果:3.14003 另一種辦法: import java.text.DecimalFormat; DecimalFormat df2 = new DecimalFormat("###.00"); DecimalFormat df2 = new DecimalFormat("###.000"); System.out.println(df2.format(f1)); 第一個為2位,第二個為3位. 如 3.14003 保留1位結果:3.1 保留2位結果:3.14 保留3位結果:3.140 保留4位結果:3.1400 保留5位結果:3.14003?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的ACM与Java -- 大整数类的常用函数一览表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021年有哪些值得关注的投资机会?20
- 下一篇: 【牛客 - 185F】 假的数学游戏(斯