A + B Problem II

来源:互联网 发布:哈尔滨数据开放平台 编辑:程序博客网 时间:2024/05/01 12:50
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
21 2112233445566778899 998877665544332211
 

Sample Output
Case 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110
 
#include <iostream>#include <cstdio>using namespace std;int main(){char s1[1007],s2[1007];int num1[1007],num2[1007];int k;int i;while(cin>>k){for(int l=1;l<=k;l++){memset(num1,0,sizeof(num1));memset(num2,0,sizeof(num2));cin>>s1>>s2;int len1=strlen(s1);int len2=strlen(s2);cout<<"Case "<<l<<":"<<endl;for(i=0;i<len1;i++){num1[i]=s1[len1-i-1]-'0';}for(i=0;i<len1;i++)                cout<<num1[len1-i-1];cout<<" + ";for(i=0;i<len2;i++){num2[i]=s2[len2-i-1]-'0';}for(i=0;i<len2;i++)                cout<<num2[len2-i-1];cout<<" = ";for(i=0;i<1000;i++){num1[i]+=num2[i];if(num1[i]>=10)                {                    num1[i]-=10;                    num1[i+1]++;                }}int flag=0;for(int j=1000;j>=0;j--){if(num1[j]!=0)                {                    flag=j;break;                }}for(int j=flag;j>=0;j--)cout<<num1[j];cout<<endl;if(l!=k)                cout<<endl;}}return 0;}

原创粉丝点击