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");
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值