两个无穷大的数相加的有关问题

两个无穷大的数相加的问题
这是原题, 哪位高手能知道一下啊?
 


A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11377 Accepted Submission(s): 1904


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

 

Sample Input
2
1 2
112233445566778899 998877665544332211
 

Sample Output
Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
 

Author
Ignatius.L
 


------解决方案--------------------
C/C++ code
#include<stdio.h>
#include<string.h>
int main()
{
    char str1[1000],str2[1000];     
    int a[1000],b[1000],c[1001];
    int N;
    int m,n,i,j,s,x,k,h,t;
    int flag=0;
    int len1,len2,len;

    scanf("%d",&N);
    for(i=1;i<=N;++i)
    {
        scanf("%s %s",&str1,&str2);
        printf("Case ");
        printf("%d",i);
        printf(":\n");
        printf("%s + %s = ",str1,str2);

        len1=strlen(str1); 
        len2=strlen(str2);

        for(s=len1-1,m=0;s>=0;s--)
        {
            a[m]=str1[s]-'0';
            m++;
        }
        for(j=len2-1,n=0;j>=0;j--)
        {
            b[n]=str2[j]-'0';
            n++;
        }
        for(h=len1;h<1000;h++)
            a[h]=0;
        for(t=len2;t<1000;t++)
            b[t]=0;
        if(len1>len2)
            len=len1;
        else if(len1=len2)
            len=len1;
        else
            len=len2;
        for(s=0;s<len;s++)
        {
            x=a[s]+b[s]+flag;
            if(x>=10)
            {
                c[s]=x%10;
                flag=1;
            }
            else
            {
                c[s]=x;
                flag=0;
            }
        }
        if(flag==1)
            printf("1");
        for(k=len-1;k>=0;k--)
            printf("%d",c[k]);    
        printf("\n");
        if (i<N)
            printf("\n");
    }
    return 0;
}

------解决方案--------------------
C/C++ code


#include <stdio.h> 
#include <string.h> 

int main(){ 
    char a[1005],b[1005]; 
    int kase,ase=1;;
    scanf("%d",&kase);
    while (kase--){
        
//    while (scanf("%s",a)!=EOF){ 
        scanf ("%s%s",a,b); 
        if (ase>1)  printf("\n");
        printf("Case %d:\n",ase++);
        
        int la,lb; 
        la=strlen(a); 
        lb=strlen(b); 
        printf("%s + %s = ",a,b);
        char c[2020]; 
        int i,j,k=0,jw=0; 
        for (i=la-1,j=lb-1;i>=0&&j>=0;i--,j--){ 
            c[k++]=(a[i]-'0'+b[j]-'0'+jw)%10+'0'; 
            jw=(a[i]-'0'+b[j]-'0'+jw)/10; 
        } 
        if (i==-1){ 
            for (j;j>=0;j--){ 
                c[k++]=(b[j]-'0'+jw)%10+'0'; 
                jw=(b[j]-'0'+jw)/10; 
            } 
        } 
        else { 
            for (i;i>=0;i--){ 
                c[k++]=(a[i]-'0'+jw)%10+'0'; 
                jw=(a[i]-'0'+jw)/10; 
            } 
        } 
        if(jw!=0) 
        printf ("%d",jw); 
        for (k-=1;k>=0;k--) 
        printf ("%d",c[k]-'0'); 
        printf("\n"); 
    } 
return 0 ;  
}