# 1、高精度运算

# AcWing 791. 高精度加法

# AcWing 792. 高精度减法

# AcWing 793. 高精度乘法

# AcWing 794. 高精度除法

Java中有两个类可以来处理高精度的计算 分别是处理整数的BigInteger和处理小数的BigDecimal

BigInteger 只可用于整数 构造方法

BigInteger(byte[] val) 将包含BigInteger的二进制补码二进制表达式的字节数组转换为BigInteger BigInteger(int numBits, Random rnd) 构造一个随机生成的BigInteger,均匀分布在0到(2 numBits - 1)的范围内。
BigInteger(String val) 将BigInteger的十进制字符串表示形式转换为BigInteger。

加法 add( )

import java.math.BigInteger;
import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        BigInteger a = new BigInteger(reader.readLine());
        BigInteger b = new BigInteger(reader.readLine());
        System.out.println(a.add(b));
        reader.close();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

减法 subtract( )

import java.io.*;
import java.math.BigInteger;

public class Main {

    public static void main(String[] args) throws IOException{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BigInteger a = new BigInteger(reader.readLine());
        BigInteger b = new BigInteger(reader.readLine());
        System.out.println(a.subtract(b));
        reader.close();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

乘法 multiply( )

import java.io.*;
import java.math.BigInteger;

public class Main {

    public static void main(String[] args) throws IOException{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BigInteger a = new BigInteger(reader.readLine());
        BigInteger b = new BigInteger(reader.readLine());
        System.out.println(a.multiply(b));
        reader.close();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

除法 divideAndRemainder( )

import java.io.*;
import java.math.BigInteger;

public class Main {

    public static void main(String[] args) throws IOException{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BigInteger a = new BigInteger(reader.readLine());
        BigInteger b = new BigInteger(reader.readLine());
        //divide 返回值为 a/b
        BigInteger[] c = a.divideAndRemainder(b); //返回值为数组,分别为a/b和a%b
        System.out.println(c[0]);
        System.out.println(c[1]);
        reader.close();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

取余 mod( )

import java.io.*;
import java.math.BigInteger;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BigInteger a = new BigInteger(reader.readLine());
        BigInteger b = new BigInteger(reader.readLine());
        System.out.println(a.mod(b));
        reader.close();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

BigDecimal 处理浮点数运算 构造方法

BigDecimal(char[] in) 一个转换的字符数组表示 BigDecimal成 BigDecimal ,接受字符作为的相同序列 BigDecimal(String)构造。
BigDecimal(char[] in, int offset, int len) 一个转换的字符数组表示 BigDecimal成 BigDecimal ,接受字符作为的相同序列 BigDecimal(String)构造,同时允许一个子阵列被指定。
BigDecimal(double val) 将 double转换为 BigDecimal ,这是 double的二进制浮点值的精确十进制表示 BigDecimal(int val) 将 int成 BigDecimal BigDecimal(long val) 将 long成 BigDecimal BigDecimal(String val)

加法 add( )

import java.io.*;
import java.math.BigDecimal;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BigDecimal a = new BigDecimal(reader.readLine());
        BigDecimal b = new BigDecimal(reader.readLine());
        System.out.println(a.add(b));
        reader.close();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

取余 remainder( )

import java.io.*;
import java.math.BigDecimal;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BigDecimal a = new BigDecimal(reader.readLine());
        BigDecimal b = new BigDecimal(reader.readLine());
        System.out.println(a.remainder(b));
        reader.close();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

除法 divide( )

import java.io.*;
import java.math.BigDecimal;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BigDecimal a = new BigDecimal(reader.readLine());
        BigDecimal b = new BigDecimal(reader.readLine());
        System.out.println(a.divide(b));
        reader.close();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13