1002

=======================================================================================
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

===========================================================================================
C++代码:

 

#include <iostream> 
#include <string.h> 
using namespace std; 
const int Len=102; 
int a[Len],b[Len]; 

void main() 
{ 
    void plus(char* st1,char* st2,int k); 
    char st1[20][102],st2[20][102]; //用二维数组来解决输入20次的问题``` 
    int k,n; 
    cin>>n; 
    if((1<=n)&&(n<=20)) 
    { 
        for(k=0;k<n;k++)      //循环输入K组数据 
        { 
            cin>>st1[k]>>st2[k];    
        } 
        for(k=0;k<n;k++)    //循环输出结果 
        { 
            plus(st1[k],st2[k],k); 
        }     
    } 
} 

void plus(char* st1,char* st2,int k) 
{ 
    int i,j,L1,L2; 
    L1=strlen(st1); 
    L2=strlen(st2);
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    for (i=0;i <L1;i++) a[Len-L1+i]=st1[i]-48;//字符串转整型 
    for (i=0;i <L2;i++) b[Len-L2+i]=st2[i]-48;//字符串转整型    
    for (i=0;i <Len;i++) a[i]=a[i]+b[i]; 
    for (i=Len-1;i>=1;i--) //数位匹配 
    { 
        a[i-1]=a[i-1]+a[i]/10; 
        a[i]=a[i]%10; 
    }    
    i=0; 
    while (a[i]==0 && i+1 <Len) i++; //将i移到数组有数字的那位 
    cout <<"case " <<k+1 <<":" <<endl; 
    cout <<st1<<"+"<<st2<<"="; 
    for (j=i;j <Len;j++) cout<<a[j]; //所求的和
    cout<<endl; 
    cout<<endl;
} 
posted @ 2011-04-11 23:59  左手写诗  阅读(321)  评论(0编辑  收藏  举报