杭电 oj 1002 A + B Problem II 记录

来源:互联网 发布:高清混合矩阵价格 编辑:程序博客网 时间:2024/06/06 02:35

   这道题,我想考的应该是大数的处理,提交的时候可谓很艰辛啊,格式不对,每次都说,最后在百度的情况下把输出格式改对了。


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
21 2112233445566778899 998877665544332211
 

Sample Output
Case 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110
 

Author
Ignatius.L
 


意思是第一行输入要输入几组测试数据,剩下的就是要输入的数了,然后计算结果,大数就用数组来装,以小学的竖式的方式来计算就好了。


要注意格式啊,格式看来对OJ来说很有重要啊。最后我提交成功的代码是这样的


#include<stdio.h>#include <string.h> #define MAX 1001int main(){char str1[MAX];char str2[MAX];int  first[MAX];int  sum[MAX];int  fl;int  sl;int  gl;int Case=0;int temp = 0;int sec = 0;int i = 0;int r = 1;scanf("%d",&Case);while(Case--){memset(first,0,sizeof(first));memset(sum,0,sizeof(sum));memset(str1,0,sizeof(str1));memset(str2,0,sizeof(str2));scanf("%s %s",str1,str2);fl = strlen(str1);sl = strlen(str2);if(fl > sl){for(i = 0;i < fl;i++){first[i] = str1[fl-i-1] - '0';}for(i = 0;i < fl;i++){if(0 <= sl-i-1)temp = first[i] + str2[sl-i-1]-'0'+sum[i];elsetemp = first[i] + sum[i];if(temp > 9){sec = temp / 10;temp = temp % 10;}sum[i] = temp;sum[i+1] = sec;sec = 0;}}else{for(i = 0;i < sl;i++){first[i] = str2[sl-i-1] - '0';}for(i = 0;i < sl;i++){if(0 <= fl-i-1){temp = first[i] + str1[fl-i-1]-'0' +sum[i];} else{temp = first[i]+sum[i];}if(temp > 9){sec = temp / 10;temp = temp % 10;}sum[i] = temp;sum[i+1] = sec;sec=0;}}if(sum[i] != 0)gl = i;else gl = i-1;printf("Case %d:\n",r);printf("%s + %s = ",str1,str2);for(i = gl;i >= 0;i--)printf("%d",sum[i]);if(Case != 0) printf("\n\n");else printf("\n");r++; }return 0;} 




可能有点乱,唉,先记录一下再说。


坚持每一天啊。加油


0 0
原创粉丝点击