NYOJ 103 a+b Problem(2)

#include<stdio.h>
#include<string.h>
int main()
{
  char str1[1001],str2[1002];
  int i,t,j=0;
  scanf("%d\n",&t);
  while(t--)
  {
    scanf("%s%s",str1,str2);
    int a[1001]={0},b[1001]={0},c[1001]={0};
    int m,n,max=1;
    m=strlen(str1);
    n=strlen(str2);
    max=m>n?m:n;
    for(i=0;i<max;i++)
    {
      a[m-1-i]=str1[i]-48;
      b[n-1-i]=str2[i]-48;
    }
    for(i=0;i<max;i++)
      c[i]=a[i]+b[i];
    for(i=0;i<max;i++)
    {
      c[i+1]=c[i]/10+c[i+1];
      c[i]=c[i]%10;
    }
    printf("Case %d:\n",++j);
    printf("%s + %s = ",str1,str2);
    if(c[max]!=0)
    {
      for(i=max;i>=0;i--)
        printf("%d",c[i]);
    }
    else
    {
      for(i=max-1;i>=0;i--)
        printf("%d",c[i]);
    }
    printf("\n");
  }
}

posted @ 2015-07-02 20:02  小松鼠。  阅读(114)  评论(0编辑  收藏  举报