hdu 1002 A+B Problem 2

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

 

分析:高精度大数加法,肯定用数组来解决,注意小坑。。。

#include <stdio.h>
#include <string.h>
#define MAX_LENTH 1001
int main()
{
    char s1[MAX_LENTH],s2[MAX_LENTH];
    int a[MAX_LENTH],b[MAX_LENTH],c[MAX_LENTH];   //a用来储存第一个数,b第二个数,c是a+b;
    int n,i,j,l1,l2,z;
    scanf("%d",&n);
    for(i=0;i<n;i++)                              //i表示第几组数据
    {
        for(j=0;j<MAX_LENTH;j++) a[j]=0;
        for(j=0;j<MAX_LENTH;j++) b[j]=0;
        for(j=0;j<MAX_LENTH;j++) c[j]=0;
        scanf("%s%s",s1,s2);                     //用字符串来读入数据
        l1=strlen(s1);
        l2=strlen(s2);
        for(j=0;j<l1;j++)   a[j]=s1[l1-1-j]-'0';
        for(j=0;j<l2;j++)   b[j]=s2[l2-1-j]-'0';
        for(j=0;j<MAX_LENTH;j++)
            c[j]=a[j]+b[j];
        for(j=0;j<MAX_LENTH;j++)
        {                                         //处理进位
            if(c[j]>=10)
            {
                c[j+1]+=c[j]/10;
                c[j]=c[j]%10;
            }
        }
        printf( "Case %d:\n",i+1);
        printf("%s + %s = ",s1,s2);
        z=0;                                      //确定首位
        for(j=MAX_LENTH-1;j>=0;j--)
        {
            if(z==0)
            {
                if(c[j]!=0){
                    printf("%d",c[j]);
                    z=1;
                }
            }
            else
                printf("%d",c[j]);
        }
        if(z==0)                            //和为0
            printf("0");
        if(i<n-1)
            printf("\n\n");
        else
            printf("\n");                //坑爹
    }
    return 0;
}
posted @ 2015-12-23 15:52  小白酷狗  阅读(281)  评论(0编辑  收藏  举报