首页 > 代码库 > A+B(大数)

A+B(大数)

Problem Description

给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开。
现在请计算A+B的结果,并以正常形式输出。

Input

输入包含多组数据数据,每组数据占一行,由两个整数A和B组成(-10^9 < A,B < 10^9)。

Output

请计算A+B的结果,并以正常形式输出,每组数据占一行。

Sample Input

-234,567,890 123,456,789
1,234 2,345,678

Sample Output

-111111101
2346912

分析:这道题目用java显然比C或c++简单的多,注意混合加减法,其实就是大数算法的灵活运用。
AC源代码(Java语言):
技术分享
import java.util.Arrays;
import java.util.Scanner;

public class Main{
//    public final static double pi = 3.1415927;
    public static void main(String[] args) {
        
        Scanner sin=new Scanner(System.in);
        while(sin.hasNext()){
            String init = null;
            String result = null;
            
            int val = 0;
            init = sin.nextLine();
            String arr[] = init.split(" ");
            String arra = arr[0].replace(",", "");
            String arrb = arr[1].replace(",", "");
            if(arra.startsWith("-")||arrb.startsWith("-")){
                if(arra.startsWith("-")&&arrb.startsWith("-")){
                    arra = arra.replace("-", "");
                    arrb = arrb.replace("-", "");
                    result = bigNumberAdd(arra, arrb);
                    System.out.println("-"+result);
                }else{
                    if(arra.startsWith("-")){
//                        System.out.println("*****");
                        arra = arra.replace("-", "");
                        result = bigNumberSub(arrb, arra);
                    }else{
                        arrb = arrb.replace("-", "");
                        result = bigNumberSub(arra, arrb);
                    }
                    System.out.println(result);
                }
            }else{
                result = bigNumberAdd(arra, arrb);
                System.out.println(result);
            }
        }
    }
    static String bigNumberAdd(String f, String s) {  
        char[] a = new StringBuffer(f).reverse().toString().toCharArray();  
        char[] b = new StringBuffer(s).reverse().toString().toCharArray();  
        int lenA = a.length;
        int lenB = b.length;
        int len = lenA > lenB ? lenA : lenB;  
        int[] result = new int[len + 1];
        for (int i = 0; i < len + 1; i++) {
            int aint = i < lenA ? (a[i] - ‘0‘) : 0; 
            int bint = i < lenB ? (b[i] - ‘0‘) : 0;  
            result[i] = aint + bint;
        }
//        for(int i=0; i<result.length; i++){
//            System.out.print(result[i]+" ");
//        }
        for (int i = 0; i <result.length; i++) {
            if (result[i] >= 10) {
                result[i + 1] += result[i] / 10;
            }
            result[i] = result[i] % 10;
        }
//        System.out.println();
//        for(int i=0; i<result.length; i++){
//            System.out.print(result[i]+" ");
//        }
//        System.out.println();
        StringBuffer sb = new StringBuffer();
        boolean flag = true;  
        for (int i = len; i >= 0; i--) {
            if (result[i] == 0 && flag) {
                continue;
            } else {
                flag = false;
            }
            sb.append(result[i]);  
        }
        if (sb.toString().equals("")) {  
            sb.append("0");  
        }
        return sb.toString();  
    }
    public static String bigNumberSub(String f, String s) {
//        System.out.println(f);
//        System.out.println(s);
        char[] a = new StringBuffer(f).reverse().toString().toCharArray();  
        char[] b = new StringBuffer(s).reverse().toString().toCharArray();  
        int lenA = a.length;
        int lenB = b.length;
//        System.out.println("lena="+lenA+",lenb="+lenB);
        int len = lenA > lenB ? lenA : lenB;  
        int[] result = new int[len];
        char sign = ‘+‘;
        if (lenA < lenB) {  
            sign = ‘-‘;
        } else if (lenA == lenB) {  
            int i = lenA - 1;  
            while (i > 0 && a[i] == b[i]) {
                i--;  
            }
            if (a[i] < b[i]) {
                sign = ‘-‘;
            }
        }
        for (int i = 0; i < len; i++) {  
            int aint = i < lenA ? (a[i] - ‘0‘) : 0;  
            int bint = i < lenB ? (b[i] - ‘0‘) : 0;  
            if (sign == ‘+‘) {  
                result[i] = aint - bint;  
            } else {
                result[i] = bint - aint;  
            }  
        }
//        for(int i=0; i<len; i++){
//            System.out.print(result[i]+" ");
//        }
        for (int i = 0; i < result.length - 1; i++) {  
            if (result[i] < 0) {  
                result[i + 1] -= 1;  
                result[i] += 10;  
            }  
        }  
  
        StringBuffer sb = new StringBuffer();
        if (sign == ‘-‘) {  
            sb.append(‘-‘);  
        }
        boolean flag = true;  
        for (int i = len - 1; i >= 0; i--) {  
            if (result[i] == 0 && flag) {  
                continue;  
            } else {  
                flag = false;  
            }  
            sb.append(result[i]);  
        }
        if (sb.toString().equals("")) {  
            sb.append("0");  
        }
//        System.out.println(sb.toString()); 
        return sb.toString();  
    }  
}
View Code

 

A+B(大数)