0
点赞
收藏
分享

微信扫一扫

ACM与Java -- 大整数类的常用函数一览表

ZGtheGreat 2022-06-20 阅读 78

​​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=0
System.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的构造器,否则相当于是int
BigDecimal 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));// 0
System.out.println(a.compareTo(b));// -1
System.out.println(b.compareTo(a));// 1

BigDecimal 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

 


举报

相关推荐

0 条评论