Basic usage of computing large numbers of Java BigInteger

Basic usage of computing large numbers of Java BigInteger

Precision operational problems encountered in the programming contest, C ++ is not precision operational, only manually simulate artificial operation, manual high precision, while the java.mathpackage BigIntegerprovides basic arithmetic precision, so the race to solve common Java precision operational problems.

Of course, if the game supports pythonit when I did.

Create a BigInteger object

BigInteger class java.math.BigInteger package, the package is first referenced.

import java.math.BigInteger;

Create a BigInteger object

BigInteger a = new BigInteger("123"); // 这里是字符串

Change the value of the BigInteger

String str = "123";
BigInteger a = BigInteger.valueOf(str);

int num = 456;
BigInteger a = BigInteger.valueOf(num);

The basic constants

a = BigInteger.ONE // 1
b = BigInteger.TEN // 10
c = BigInteger.ZERO // 0

BigInteger input and output

Read directly into BigInteger

Scanner in = new Scanner(System.in); 
while(in.hasNext()) //等同于!=EOF
{
    BigInteger a;
    a = in.nextBigInteger();
    System.out.print(a.toString());
}

Indirect read BigInteger

Scanner in = new Scanner(System.in); 
while(in.hasNext()) //等同于!=EOF
{
    String s = in.nextLine();
    BigInteger a = new BigInteger(s);
    System.out.print(a.toString());
}

BigInteger direct output

System.out.print(a);

String BigInteger converted to decimal notation

System.out.print(a.toString());

BigInteger converted into a binary representation of p String

int p = 2;
System.out.print(a.toString(p)); // 输出a的二进制

Length in binary BigInteger

BigInteger n = new BigInteger("12");
System.out.println(n.bitLength()); // 4

The basic operation of BigInteger

Comparison between the BigInteger

BigInteger a = new BigInteger("123");
BigInteger b = new BigInteger("456");

System.out.println(a.equals(b)); // a == b 时为 true 否则为 false
BigInteger a = new BigInteger("123");
BigInteger b = new BigInteger("456");

if(a.compareTo(b) == 0) System.out.println("a == b"); // a == b
else if(a.compareTo(b) > 0) System.out.println("a > b"); // a > b
else if(a.compareTo(b) < 0) System.out.println("a < b"); // a < b

addition

BigInteger a = new BigInteger("123");
BigInteger b = new BigInteger("456");

a.add(b);

Subtraction

BigInteger a = new BigInteger("123");
BigInteger b = new BigInteger("456");

a.subtract(b);

multiplication

BigInteger a = new BigInteger("123");
BigInteger b = new BigInteger("456");

a.multiply(b);

division

BigInteger a = new BigInteger("123");
BigInteger b = new BigInteger("456");

a.divide(b);

Remainder

BigInteger a = new BigInteger("123");
BigInteger b = new BigInteger("456");

BigInteger c = b.remainder(a);
System.out.println(c);

Modulo division

BigInteger a = new BigInteger("123");
BigInteger b = new BigInteger("456");

BigInteger result[] = b.divideAndRemainder(a); // 该函数返回的是数组
System.out.println("商是:" + result[0] + ";余数是:" + result[1]);

Greatest common divisor

BigInteger a = new BigInteger("12");
BigInteger b = new BigInteger("56");
        
System.out.println(a.gcd(b)); // 4

Absolute value

BigInteger a = new BigInteger("-12");
        
System.out.println(a.abs()); // 12

Invert number

BigInteger a = new BigInteger("-12");
        
System.out.println(a.negate()); // 12

power

BigInteger a = new BigInteger("2");
        
System.out.println(a.pow(3)); // 8

Guess you like

Origin www.cnblogs.com/wulitaotao/p/11360386.html