您的位置:首页 > 其它

杭电OJ-1001-(Sum Problem大整数求和问题)

2015-08-03 16:20 239 查看
Problem Description:

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

Input:

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases.

Then T lines follow, each line consists of two positive integers, A and B.

Notice that the integers are very large, that means you should not process them by using 32-bit integer.

You may assume the length of each integer will not exceed 1000.

Output:

For each test case, you should output two lines.

The first line is “Case #:”, # means the number of the test case.

The second line is the an equation “A + B = Sum”, Sum means the result of A + B.

Note there are some spaces in the equation. Output a blank line between two test cases.

Sample Input:

4

1 2

5 5

112233445566778899 998877665544332211

112233445566778893355877665544332211998877665544332211112233445566778823 998877665544332211112233445566778823112233445566778893355877665544332211

Sample Output:

Case 1:

1 + 2 = 3

Case 2:

5 + 5 = 10

Case 3:

112233445566778899 + 998877665544332211 = 1111111111111111110

Case 4:

112233445566778893355877665544332211998877665544332211112233445566778823 + 998877665544332211112233445566778823112233445566778893355877665544332211 = 1111111111111111104468111111111111035111111111111111104468111111111111034

解法1:

利用Java语言的良好特性:

[code]import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int len =0;
        while(input.hasNext()){
            len = input.nextInt();
            for(int i=1 ; i<= len ;i++){
                BigInteger a = new BigInteger(input.next());
                BigInteger b = new BigInteger(input.next());
                System.out.println("Case "+i+":");
                System.out.println(a+" + "+b+" = "+a.add(b));
                if(i<=len-1){
                    System.out.println();
                }
            }
        }
    }
}


解法2:

[code]import java.util.Scanner;
public class Main{
    static int N=1000; 
    public static void main(String[] args) {  
        int a[];  
        int b[];
        Scanner input=new Scanner(System.in);  
        while(input.hasNext()){
            int len = input.nextInt();
            for(int i=1 ; i<= len ; i++){
                a = getDigit(input.next());  
                b = getDigit(input.next()); 
                System.out.println("Case "+i+":");
                System.out.println(getString(a)+" + "+ getString(b)+" = "+ getString(add(a,b)));
                if(i<=len-1){
                    System.out.println();
                }
            }
        }
    }
   public static String getString(int array []){
        String str ="" ;
        int flag= array.length-1;
        while(array[flag]==0) {  
            flag--;  
            if(flag==-1) {  
                return "0" ;
            }
        }
        for(int i=flag;i>=0;i--) {  
            str += array[i];
        }
        return str;
    }

private static int[] add(int a[], int b[]) {
        int [] result = new int [N+1];
        for(int i=0; i<N ;i++) {  
            result[i]=a[i]+b[i];  //先将每个数组对应位上做加法
        }
        for(int i=0; i<N ;i++) {  
            result[i+1] += result[i]/10; //加法进位  
            result[i] = result[i]%10;  //余数
        }
        return result;
    }  

private static int[] getDigit(String s) {  
        int array [] = new int 
;
        int len= s.length();
        for(int i=0;i<len;i++) {  
            array[i]=s.charAt(len-1-i)-'0';  
        }
        return array;
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: