高精度加减乘除Java实现(acwing)

高精度加法

import java.util.*;
import java.io.*;
public class Main{
    static List<Integer> add(List<Integer> A, List<Integer> B){
        List<Integer> C = new ArrayList<>();
        int t = 0;
        for(int i = 0; i < A.size() || i < B.size(); i ++){
            if(i < A.size()) t += A.get(i);
            if(i < B.size()) t += B.get(i);
            C.add(t % 10);
            t = t / 10;
        }
        if(t != 0) C.add(t);
        return C;
    }
    public static void main(String[] args) throws IOException{
        List<Integer> A = new ArrayList<>();
        List<Integer> B = new ArrayList<>();
        String a, b;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        a = br.readLine();
        b = br.readLine();
        br.close();
        for(int i = a.length() - 1; i >= 0; i --){
            A.add(a.charAt(i) - '0');
        }
        for(int i = b.length() - 1; i >= 0; i --){
            B.add(b.charAt(i) - '0');
        }
        List<Integer> C = add(A, B);
        for(int i = C.size() - 1; i >= 0; i --){
            System.out.print(C.get(i));
        }
    }
}

高精度减法

import java.util.*;
import java.io.*;
public class Main{
    static Boolean compare(List<Integer> A, List<Integer> B){
        if(A.size() == B.size()){
            for(int i = A.size() - 1; i >= 0; i --){
                if(A.get(i) != B.get(i)){
                    return A.get(i) > B.get(i);
                }
            }
        }else{
            return A.size() > B.size(); 
        }
        return true;
    }
    static List<Integer> del(List<Integer> A, List<Integer> B){
        List<Integer> C = new ArrayList<>();
        int t = 0;
        for(int i = 0; i < A.size() || i < B.size(); i ++){
            if(i < A.size()) t += A.get(i);
            if(i < B.size()) t -= B.get(i);
            if(t < 0){
                C.add(t + 10);
                t = -1;
            }else{
                C.add(t);
                t = 0;
            }
        }
        int j = C.size() - 1;
        while(C.get(j) == 0 && j > 0){
            C.remove(j);
            j --;
        }
        return C;
    }
    public static void main(String[] args) throws IOException{
        List<Integer> A = new ArrayList<>();
        List<Integer> B = new ArrayList<>();
        String a, b;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        a = br.readLine();
        b = br.readLine();
        br.close();
        for(int i = a.length() - 1; i >= 0; i --){
            A.add(a.charAt(i) - '0');
        }
        for(int i = b.length() - 1; i >= 0; i --){
            B.add(b.charAt(i) - '0');
        }
        List<Integer> C;
        if(compare(A, B)){
            C = del(A, B);
        }else{
            System.out.print("-");
            C = del(B, A);
        }
        for(int i = C.size() - 1; i >= 0; i --){
            System.out.print(C.get(i));
        }
    }
}

高精度乘法

import java.util.*;
public class Main{
    static List<Integer> mul(List<Integer> A, int b){
        List<Integer> C = new ArrayList<>();
        int t = 0;
        for(int i = 0; i < A.size() || t != 0; i ++){
            if(i < A.size()){
                t += A.get(i) * b;
            }
            C.add(t % 10);
            t /= 10;
        }
        // 去除前导零
        int j = C.size() - 1;
        while(j > 0 && C.get(j) == 0){
            C.remove(j);
            j --;
        }
        return C;
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String a = sc.next();
        int b = sc.nextInt();
        List<Integer> A = new ArrayList<>();
        for(int i = a.length() - 1; i >= 0; i --){
            A.add(a.charAt(i) - '0');
        }
        List<Integer> C = mul(A, b);
        for(int i = C.size() - 1; i >= 0; i --){
            System.out.print(C.get(i));
        }
    }
}

高精度除法

import java.util.*;
public class Main{
    static int r = 0; // 余数
    static List<Integer> dev(List<Integer> A, int b){
        List<Integer> C = new ArrayList<>();
        for(int i = 0; i < A.size(); i ++){
            r = A.get(i) + r * 10;
            C.add(r / b);
            r %= b;
        }
        int j = 0;
        while(C.size() > 1 && C.get(j) == 0){
            C.remove(j);
        }
        return C;
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String a = sc.next();
        int b = sc.nextInt();
        List<Integer> A = new ArrayList<>();
        for(int i = 0; i < a.length(); i ++){
            A.add(a.charAt(i) - '0');
        }
        List<Integer> C = dev(A, b);
        for(int i = 0; i < C.size(); i ++){
            System.out.print(C.get(i));
        }
        System.out.println();
        System.out.println(r);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值