HDU 1002 A + B Problem II

来源:互联网 发布:校园网络诈骗小品剧本 编辑:程序博客网 时间:2024/06/07 04:51

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

题意:长整数相加。

解题思路:字符串输入,处理数据。

AC代码:

#include<stdio.h>#include<string.h>char a[1010],b[1010];int main(){    int t,q=1,l,h,i,j,k;    scanf("%d",&t);    while(t--)    {        int c[1010]={0},d[1010]={0},v[1010]={0};        scanf("%s",a);        scanf("%s",b);        printf("Case %d:\n",q++);        printf("%s + %s = ",a,b);        l=strlen(a);        h=strlen(b);        for(i=l-1,j=0;i>=0;i--)        {            c[j++]=a[i]-'0';        }        for(i=h-1,k=0;i>=0;i--)        {            d[k++]=b[i]-'0';        }        //printf("\n");        //for(i=0;i<l;i++)        //{        //  printf("%d",c[i]);        //}        //printf("\n");        //for(i=0;i<k;i++)        //{        //  printf("%d",d[i]);        //}        //printf("\n");        int maxx=(l>h)?l:h;        for(i=0;i<maxx-1;i++)        {            v[i]=c[i]+d[i];            if(v[i]>=10)             {                c[i+1]+=1;                v[i]-=10;            }        }        printf("%d",c[maxx-1]+d[maxx-1]);//最高一位相加的结果单独输出         for(j=maxx-2;j>=0;j--)        {            printf("%d",v[j]);        }        printf("\n");        if(t) printf("\n");    }    return 0;}
#include<stdio.h>#include<string.h>char a[1010],b[1010];int main(){    int t,q=1,l,h,i,j,k;    scanf("%d",&t);    while(t--)    {        int c[1010]={0},d[1010]={0},v[1010]={0};        scanf("%s",a);        scanf("%s",b);        printf("Case %d:\n",q++);        printf("%s + %s = ",a,b);        l=strlen(a);        h=strlen(b);        for(i=l-1,j=0;i>=0;i--)        {            c[j++]=a[i]-'0';        }        for(i=h-1,k=0;i>=0;i--)        {            d[k++]=b[i]-'0';        }        //printf("\n");        //for(i=0;i<l;i++)        //{        //  printf("%d",c[i]);        //}        //printf("\n");        //for(i=0;i<k;i++)        //{        //  printf("%d",d[i]);        //}        //printf("\n");        int maxx=(l>h)?l:h;        for(i=0;i<=maxx;i++)        {            v[i]=c[i]+d[i];            if(v[i]>=10)             {                c[i+1]+=1;                v[i]-=10;            }        }        if(v[maxx]) printf("%d",v[maxx]);//最高一位相加的结果是两位数则maxx上有值         for(i=maxx-1;i>=0;i--)        {            printf("%d",v[i]);        }        printf("\n");        if(t) printf("\n");    }    return 0;}

还能更好吗?