HDU 1002 A + B Problem II(大数据)
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
因为数字超级大,所以就算long long 也是不行的,所以用字符数组输入,然后再转化成数字,然后进行加法进位运算。
要注意PE
AC代码
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int t,len1,len2; char a[1010],b[1010]; int na[1010],nb[1010],ans[1010]; int main(){ scanf("%d",&t); int tt=t; int cnt=1; while(t--){ scanf("%s%s",a,b); len1=strlen(a); len2=strlen(b); memset(na,0,sizeof(na));//初始化清零 memset(nb,0,sizeof(nb)); memset(ans,0,sizeof(ans)); for(int i=len1-1;i>=0;i--)//将字符转为数字 na[len1-1-i]=a[i]-'0';//要逆着来,为了后面加法进位 for(int i=len2-1;i>=0;i--) nb[len2-1-i]=b[i]-'0'; int i,k=0; for(i=0;i<max(len1,len2);i++){ k=na[i]+nb[i]+k; ans[i]=k%10; //进位 k=k/10; } if(k!=0) ans[i++]=k; printf("Case %d:\n%s + %s = ",cnt++,a,b); int p=0; if(i==1&&ans[0]==0){ //如果不写这个,0+0就不输出了 printf("0\n"); if((cnt-1)!=tt) printf("\n"); continue; } for(int j=i-1;j>=0;j--){ if(p==0&&ans[j]==0){ //前导零,如0003+2 continue; } else{ p=1; printf("%d",ans[j]); } } printf("\n"); if((cnt-1)!=tt) printf("\n"); //PE了好几次,它要两个测试之间有空行,最后一组不能有空行 } }
(突然想起来,在每篇博客后面写一句自己的话才是我的风格嘛)
(私はカメラですね)
posted on 2018-12-29 16:43 甜甜圈不懂巧克力的苦 阅读(213) 评论(0) 编辑 收藏 举报