A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 278225 Accepted Submission(s): 53657
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 /*........................................上面是题目,下面是我的解答.........................................................*/ /*2015.11.5 馒头作业*/ #include<stdio.h> #include<string.h> int s[2][1005]={0}; char S[2][1005]; int main() { int n=0,i=0; scanf("%d
",&n); int z=0,first=1;/*first用于计录第几次*/ for(z=0;z<n;z++)/*进行n次计算*/ { memset(S,'0',sizeof(S));/*字符清零*/ memset(s,0,sizeof(s)); for(i=0;i<1006;i++) { scanf("%c",&S[0][i]); if(S[0][i]==' '||S[0][i]=='
') { S[0][i]=' '; break; } } for(i=0;i<1006;i++) { scanf("%c",&S[1][i]); if(S[1][i]==' '||S[1][i]=='
') { S[1][i]=' '; break; } } int n1=0,n2=0; n1=strlen(S[0]); n2=strlen(S[1]); /*.......................接下来将字符变为数字...........................................*/ for(i=0;i<n1;i++) { s[0][i]=S[0][i]-'0'; } for(i=0;i<n2;i++) { s[1][i]=S[1][i]-'0'; } /*.........................倒转,准备加法进位........................................*/ int t=0; for(i=0;i<n1/2;i++) { t=s[0][i]; s[0][i]=s[0][n1-1-i]; s[0][n1-1-i]=t; } for(i=0;i<n2/2;i++) { t=s[1][i]; s[1][i]=s[1][n2-1-i]; s[1][n2-1-i]=t; } /*......................相加,进位...........................................*/ int max=0; max=n1>n2?n1:n2; for(i=0;i<max;i++) { s[0][i]=s[0][i]+s[1][i]; if(s[0][i]>9) { s[0][i]%=10; s[0][i+1]++; } } /*.........................找到最高位不为零的那一项,标记.........................................*/ int k=0; for(i=max+1;i>=0;i--) { if(s[0][i]!=0) { k=i; break; } } /*......................按照格式输出..............................................*/ printf("Case %d:
",first); for(i=0;i<n1;i++) printf("%c",S[0][i]); printf(" + "); for(i=0;i<n2;i++) printf("%c",S[1][i]); printf(" = "); for(i=k;i>=0;i--) { printf("%d",s[0][i]); } printf("
"); first++; if(first-1!=n) printf("
"); } return 0; } 为什么过不了......太伤心了,打了那么久= =