java 高精度 加减乘除

这里写目录标题

减法

使用 java 大数类

import java.math.BigInteger;
import java.util.*;

public class Main {
    
          //高精度减法
    public static void main(String[] args) {
    
    
        Scanner mc = new Scanner(System.in);

        String[] s = new String[10086];
        for (int b = 0; b < 2; b++) {
    
    
            s[b] = mc.nextLine();
        }
        BigInteger a = new BigInteger(String.valueOf(s[0]));
        BigInteger b = new BigInteger(String.valueOf(s[1]));
        BigInteger c = a.subtract(b);
        System.out.println(c);
    }
}

加法

import java.math.BigInteger;
import java.util.*;

public class Main {
    
          //高精度加法
    public static void main(String[] args) {
    
    
        Scanner mc = new Scanner(System.in);

        String a=mc.nextLine();
        String b=mc.nextLine();
        BigInteger num = new BigInteger(a);
        BigInteger num1 = new BigInteger(b);
        BigInteger c = num.add(num1);
        System.out.println(c);
    }
}

乘法

import java.math.BigInteger;
import java.util.*;

public class Main {
    
          //高精度乘法
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        BigInteger num = new BigInteger(sc.next());
        BigInteger num1 = new BigInteger(sc.next());
        System.out.println(num.multiply(num1));
    }
}

除法

import java.math.BigInteger;
import java.util.*;

public class Main {
    
          //高精度除法  赋值
    public static void main(String[] args) {
    
    
        
        BigInteger num = new BigInteger(String.valueOf("3"));
        BigInteger num1 = new BigInteger(String.valueOf("4"));
        System.out.println(num.divide(num1));
    }
}
比较大小
import java.math.BigInteger;
import java.util.*;

public class Main {
    
          //高精度 比较大小
    public static void main(String[] args) {
    
    
 Scanner mc = new Scanner(System.in);
        while (mc.hasNext()) {
    
    
            BigInteger a = mc.nextBigInteger();
            BigInteger b = mc.nextBigInteger();
            if (a.equals(BigInteger.ZERO) && b.equals(BigInteger.ZERO))
                break;
            int flag = a.compareTo(b);
            if (flag == -1)
                System.out.println("a<b");
            else if (flag == 0)
                System.out.println("a==b");
            else
                System.out.println("a>b");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qjjspl_/article/details/125346925