杭电ACM---1002(大数)

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 305668    Accepted Submission(s): 59062



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.
 

Outpu

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 int the equation. Output a blank line between two test cases.
 


Sample Inputt
    
    
    
    
2 1 2 112233445566778899 998877665544332211
 

Sample Output
    
    
    
    
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110

分析:本体表面上看只是普通的a+b题,要注意的是,输入的数可能大到整数甚至是浮点数无法表示,因此这一题有两种做法,如下。


做法一:用字符串和字符数组做

这种方法比较麻烦一些,有点绞思维

import java.util.Scanner;

class Main{
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int n=input.nextInt();
        int c=0;
        int num=0;
        while (n-->0){
        	num++;
        	String a=input.next();
        	String b=input.next();
        	char cha[]=a.toCharArray();
        	char chb[]=b.toCharArray();
        	int A[]=new int[1000];
        	int B[]=new int[1000];
        	int C[]=new int[1000];
        	for (int i=0;i<cha.length;i++){
        		A[i]=cha[cha.length-1-i]-48;
        	}
        	for (int i=0;i<chb.length;i++){
        		B[i]=chb[chb.length-1-i]-48;
        	}
        	int s=0;
        	for (int i=0;i<1000;i++){
        		s+=A[i]+B[i];
        		C[i]=s%10;
        		s/=10;
        	}
        	String str="";
        	int j;
        	for (j=999;j>=0;j--){
        		if (C[j]>0){
        			break;
        		}
        	}
        	for (int i=j;i>=0;i--){
        		str+=C[i];
        	}
        	if (c++>0){
        		System.out.println();
        	}
        	System.out.println("Case "+num+":");
        	for (int i=0;i<cha.length;i++){
        		System.out.print(cha[i]);
        	}
        	System.out.print(" + ");
        	for (int i=0;i<chb.length;i++){
        		System.out.print(chb[i]);
        	}
        	System.out.println(" = "+str);
        }
    }
}

做法二:用java的大数做

这种就非常方便,且好懂,省时(关于大数详情请见本博客“Java之------大数(BigInteger,BigDecimal)”点击打开链接)

import java.math.BigInteger;
import java.util.Scanner;

class Main{

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		int t=input.nextInt();
		int blank=0;
		int num=1;
		while (t-->0){
			if (blank++>0){
				System.out.println();
			}
			String aStr=input.next();
			String bStr=input.next();
			
			BigInteger a=new BigInteger(aStr);
			BigInteger b=new BigInteger(bStr);
			BigInteger c=a.add(b);
			
			String cStr=c.toString();
			System.out.println("Case "+num+":");
			System.out.println(aStr+" + "+bStr+" = "+cStr);
			num++;
		}
	}
}

你可能感兴趣的:(java,算法,编程语言,杭电)