无线OSS-高精度整数加法

无线OSS-高精度整数加法

http://www.nowcoder.com/questionTerminal/49e772ab08994a96980f9618892e55b6

import java.util.*;

public class Main {

    public Main() {
    }

    // return num1 > num2 ?
    private int compare(String num1, String num2) {
        if (num1.length() > num2.length()) return 1;
        if (num1.length() < num2.length()) return -1;
        for (int i = 0, j = 0; i < num1.length() && j < num2.length(); i++, j++) {
            if (num1.charAt(i) > num2.charAt(j)) {
                return 1;
            }
            if (num1.charAt(i) < num2.charAt(j)) {
                return -1;
            }
        }
        return 0;
    }

    public String sub(String num1, String num2) {
        Stack<Integer> stack = new Stack<>();
        StringBuilder res = new StringBuilder();
        // num1 > num2, so num1 - num2
        int carry = 0, i = num1.length() - 1, j = num2.length() - 1;
        while (i >= 0 || j >= 0 || carry != 0) {
            int sum = carry;
            if (i >= 0) {
                sum += num1.charAt(i--) - '0';
            }
            if (j >= 0) {
                sum -= num2.charAt(j--) - '0';
            }
            if (sum < 0) {
                stack.push(sum + 10);
                carry = -1;
            }
            else {
                stack.push(sum);
                carry = 0;
            }
        }
        while (!stack.isEmpty()) {
            res.append(stack.pop());
        }
        return res.toString();
    }

    public String add(String num1, String num2) {
        if (num1.startsWith("-") && num2.startsWith("-")) {
            num1 = num1.substring(1);
            num2 = num2.substring(1);
            return "-" + add(num1, num2);
        }
        else if (num1.startsWith("-")) {
            num1 = num1.substring(1);
            int flag = compare(num1, num2);
            if      (flag ==  1) return "-" + sub(num1, num2);
            else if (flag == -1) return sub(num2, num1);
            else                 return "0";
        }
        else if (num2.startsWith("-")) {
            // num1 - num2
            return add(num2, num1);
        }
        else {
            StringBuilder res = new StringBuilder();
            Stack<Integer> stack = new Stack<>();
            // two positive sum
            int carry = 0, i = num1.length() - 1, j = num2.length() - 1;
            while (i >= 0 || j >= 0 || carry != 0) {
                int sum = carry;
                if (i >= 0) {
                    sum += num1.charAt(i--) - '0';
                }
                if (j >= 0) {
                    sum += num2.charAt(j--) - '0';
                }
                stack.push(sum % 10);
                carry = sum / 10;
            }
            while (!stack.isEmpty()) {
                res.append(stack.pop());
            }
            return res.toString();
        }
    }

    public static void main(String[] args) {
        Main solution = new Main();
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String num1 = in.next();
            String num2 = in.next();
            String res = solution.add(num1, num2);
            System.out.println(res);
        }
    } 
}
全部评论
python 3行代码搞定
点赞 回复 分享
发布于 2023-08-03 20:20 陕西

相关推荐

贺兰星辰:不要漏个人信息,除了简历模板不太好以外你这个个人简介是不是太夸大了...
点赞 评论 收藏
分享
把球:这个听过,你加了就会发现是字节的hr
点赞 评论 收藏
分享
4 收藏 评论
分享
牛客网
牛客企业服务