高精度运算(Java)/**加法、减法、乘法、除法、取余模板*/


文章目录

  • 高精度 + 高精度
  • 高精度 - 高精度
  • 高精度 * 高精度
  • 高精度 / 高精度
  • 高精度 * 单精度
  • 高精度 / 单精度
  • 高精度幂取模
  • 高精度取余


  Java大数666


高精度 + 高精度

? 题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1005 ?

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        BigInteger a = in.nextBigInteger();
        BigInteger b = in.nextBigInteger();
        System.out.println(a.add(b));
    }
}


高精度 - 高精度

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        BigInteger a = in.nextBigInteger();
        BigInteger b = in.nextBigInteger();
        System.out.println(a.subtract(b));
    }
}


高精度 * 高精度

? 题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1027 ?

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        BigInteger a = in.nextBigInteger();
        BigInteger b = in.nextBigInteger();
        System.out.println(a.multiply(b));
    }
}


高精度 / 高精度

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        BigInteger a = in.nextBigInteger();
        BigInteger b = in.nextBigInteger();
        System.out.println(a.divide(b));
    }
}


高精度 * 单精度

? 题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1057 ?

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        BigInteger ans = BigInteger.ONE;
        for(int i = 1; i <= n; i++){
            ans = ans.multiply(new BigInteger(i + ""));
        }
        System.out.println(ans);
        int a = 2;
        System.out.println(ans.multiply(new BigInteger(a + "")));
    }
}


高精度 / 单精度

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        BigInteger ans = new BigInteger("12345678910");
        int a = 2;
        System.out.println(ans.divide(new BigInteger(a + "")));
    }
}


高精度幂取模

? 题目链接:https://ac.nowcoder.com/acm/contest/392/B ?

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        while(T-->0){
            BigInteger a = in.nextBigInteger();
            BigInteger b = in.nextBigInteger();
            BigInteger mod = in.nextBigInteger();
            BigInteger ans = a.modPow(b,mod);
            System.out.println(ans);
        }
    }
}


高精度取余

? 题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1008 ?

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int mod = in.nextInt();
        BigInteger ans = BigInteger.ONE;
        for(int i = 1; i <= n; i++){
            ans = ans.multiply(new BigInteger(i + ""));
        }
        ans = ans.remainder(new BigInteger(mod + ""));
        System.out.println(ans);
    }
}


你可能感兴趣的:(大数运算)