HDU 1002 A + B Problem II

/*高精度*/
#include <cstdio> #include <iostream> using namespace std; const int modnum=10; int main() { int n; scanf("%d",&n); int m=n; while(n--) { printf("Case %d:\n",m-n); int a[1010]={0},b[1010]={0},c[1010]={0}; char ch1[1010],ch2[1010]; scanf("%s",&ch1); scanf("%s",&ch2); a[0]=strlen(ch1); for (int i=1; i<=a[0]; i++) a[i]=ch1[a[0]-i]-'0'; b[0]=strlen(ch2); for (int i=1; i<=b[0]; i++) b[i]=ch2[b[0]-i]-'0'; int x=0,i=0; c[0]=a[0]>b[0]?a[0]:b[0]; for (int i=1;i<=c[0];i++) c[i]=a[i]+b[i]; for (int i=1;i<=c[0];i++){ c[i+1]+=c[i]/modnum; c[i]%=modnum; } c[0]++; while ((c[0] > 1) && (c[c[0]] == 0)) c[0]--; for (int i=a[0]; i; i--) printf("%d",a[i]); printf(" + "); for (int i=b[0]; i; i--) printf("%d",b[i]); printf(" = "); for (int i=c[0]; i; i--) printf("%d",c[i]); printf("\n"); if (n) printf("\n"); } return 0; }

 

posted @ 2014-01-18 10:13  forever97  阅读(207)  评论(0编辑  收藏  举报