大数A+B


给出2个大整数A,B,计算A+B的结果。


Input

第1行:大数A
第2行:大数B
(A,B的长度 <= 10000 需注意:A B有可能为负数)

Output

输出A + B

Input示例

68932147586
468711654886

Output示例

537643802472

解法1
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
        String a=reader.readLine();
        String b=reader.readLine();
        System.out.println(new BigInteger(a).add(new BigInteger(b)));
    }
}

直接用Java大数类处理,比较无赖。

解法2
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String A = reader.readLine();
        String B = reader.readLine();
        if (A.trim().equals(B.trim())) {
            System.out.println(0);
        } else {
            add(A, B);
        }
    }

    static void add(String A, String B) {
        //提取符号作判断
        char signA = A.charAt(0);
        char signB = B.charAt(0);

        //运算对象,反转易于计算
        if (signA == '-') A = A.substring(1);
        if (signB == '-') B = B.substring(1);
        char[] numA = new StringBuilder(A).reverse().toString().toCharArray();
        char[] numB = new StringBuilder(B).reverse().toString().toCharArray();

        //结果集
        int[] result = new int[A.length() > B.length() ? A.length() + 1 : B.length() + 1];

        //两者都为正数或两者都为负数
        if ((signA != '-' && signB != '-') || (signA == '-' && signB == '-')) {
            for (int i = 0; i < result.length; i++) {
                //模拟竖式相加,先不考虑进位,为了避免numA数组或numB数组下标溢出,这里需要先判定i是否超过数组下标
                result[i] = i < numA.length ? numA[i] - '0' : 0;
                result[i] += i < numB.length ? numB[i] - '0' : 0;
            }
            //进位处理
            for (int i = 0; i < result.length; i++) {
                if (result[i] >= 10) {
                    result[i + 1] += 1;
                    result[i] = result[i] % 10;
                }
            }
            //输出,要注意对第一位可能为0的处理
            StringBuilder sb = new StringBuilder();
            int i = result.length - 1;
            if (result[i] == 0) i--;
            for (; i >= 0; i--) {
                sb.append(result[i]);
            }
            //如果两者为负数需要添加负号
            if (signA != '-' && signB != '-') {
                System.out.println(sb.toString());
            } else {
                System.out.println(sb.insert(0, '-').toString());
            }
        }

        //若A为负数,B为正数
        if (signA == '-' && signB != '-') {
            exchange(numA, numB);
        }
        //若A为正数,B为负数
        if (signA != '-' && signB == '-') {
            exchange(numB, numA);
        }
    }

    /**
     * @param [numA, numB]
     * @return void
     * @descript 绝对值大小比较
     **/
    static void exchange(char[] numA, char[] numB) {
        if (numA.length > numB.length) {
            System.out.println("-" + minus(numA, numB));
        } else if (numA.length < numB.length) {
            System.out.println(minus(numB, numA));
        } else if (numA.length == numB.length) {
            int i = numA.length - 1;
            for (; i >= 0; i--) {
                if (numA[i] - numB[i] > 0) {
                    System.out.println("-" + minus(numA, numB));
                    break;
                } else if (numA[i] - numB[i] < 0) {
                    System.out.println(minus(numB, numA));
                    break;
                }
            }
        }
    }

    /**
     * @param [minuend 被减数, subtrahend 减数]
     * @return void
     * @descript 返回绝对值之差
     **/
    static String minus(char[] minuend, char[] subtrahend) {
        int[] result = new int[minuend.length];
        StringBuilder sb = new StringBuilder();
        //不考虑借位,作相减操作
        for (int i = 0; i < minuend.length; i++) {
            result[i] = i < subtrahend.length ? minuend[i] - subtrahend[i] : minuend[i] - '0';
        }
        //借位处理
        for (int i = 0; i < result.length - 1; i++) {
            if (result[i] < 0) {
                result[i] += 10;
                result[i + 1] -= 1;
            }
        }
        //处理高位为零的情况并拼接结果
        boolean over = false;
        for (int i = result.length - 1; i >= 0; i--) {
            if (result[i] == 0 && !over) {
                continue;
            }else{
                sb.append(result[i]);
                over=true;
            }
        }
        return sb.toString();
    }
}

你可能感兴趣的:(编程)