HDU 1002.A + B Problem II

来源:互联网 发布:mysql日期格式化 编辑:程序博客网 时间:2024/05/16 10:38

 

/*
A + B Problem II
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
*/

#include<stdio.h>
#include<string.h>
#define LEN 2000
int an1[LEN+10];
int an2[LEN+10];
char stra[LEN+10];
char strb[LEN+10];
int main()
{
   int t,n=0,j;
   scanf("%d",&t);
   while(n++<t)
   {
      memset(an1,0,sizeof(an1));//对数组初始化;
      memset(an2,0,sizeof(an2));
      scanf("%s", stra);
      scanf("%s", strb);
      int i, j;
      int Len1 = strlen( stra);
      for( j = 0, i = Len1 - 1;i >= 0 ; i--)  //倒序存到另一个整形数组中
         an1[j++] = stra[i] - '0';
      int Len2 = strlen(strb);
      for( j = 0, i = Len2 - 1;i >= 0 ; i--)
         an2[j++] = strb[i] - '0';
      for( i = 0;i < LEN ; i ++ )
      {  an1[i] += an2[i]; //逐位相加
           if( an1[i] >= 10 )
           { //看是否要进位
              an1[i] -= 10;
              an1[i+1] ++; //进位
           }
      }
      printf("Case %d:\n",n);//输出结果
      printf("%s + %s = ",stra,strb);
      for(j=LEN+10;an1[j]==0;j--);//从高位把c中是零的给排除
      if(j<0)
         printf("0");
      else
      for(;j>=0;j--)
      {
           printf("%d",an1[j]);
      }
      printf("\n");
      if(n<t)           // 注意题目输出格式
        printf("\n");
   }
   return 0;
}

 

 

 

0 0
原创粉丝点击