A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 316067 Accepted Submission(s): 61349
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
解读1:
将输入的两个加数整理一下,比如:
1)99 和1整理成099和001的模式
2)8 和7整理成08和07的模式
相当于模拟算式运算
【】【】【】
+【】【】【】
【】【】【】
最后输出时稍作判断即可
代码:
#include<iostream> #include<cstring> using namespace std; char a[1005],b[1005],c[1005],d[1005]; int main() { int T; cin>>T; int x,y,temp; int la,lb,l,k=0; while(T--) { k++;//计数第几个例子 cin>>a; cin>>b;//加数和被加数 la=strlen(a); lb=strlen(b);//加数和被加数的长度 l=la>lb?la:lb;//l为选取其中的最大长度 for(int i=la;i>=0;i--) a[i+l-la+1]=a[i]; for(int i=lb;i>=0;i--) b[i+l-lb+1]=b[i]; //将加数和被加数往后移以保持个位对齐 for(int i=0;i<=l-la;i++) a[i]='0'; for(int i=0;i<=l-lb;i++) b[i]='0'; //高位空缺的补'0' temp=0;//temp代表进位 c[l+1]='