A + B Problem II HDU - 1002 (大數的計算處理)

2020-08-08 13:16:52

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 <stdlib.h>
#include <string.h>
int main()
{
    char a[8000], b[8000];
    int c[8000], d[8000], sum[8000], n, flag=1;
    int i, j, k, t, len1, len2;
    scanf("%d", &t);
    while (t--)
    {
        memset(sum, 0, sizeof(sum));
        memset(c, 0, sizeof(c));
        memset(d, 0, sizeof(d));
        scanf("%s %s", a, b);
        n = 0;
        len1 = strlen(a);
        len2 = strlen(b);
        for (i=0;i<len1;i++)
            c[len1-1-i] = a[i]-'0';
        for (j=0;j<len2;j++)
            d[len2-1-j] = b[j]-'0';
        int len = len1>len2?len1:len2;
        for (k=0;k<len;k++)
        {
            sum[k] = c[k]+d[k]+n/10;
            n = sum[k];
        }
        while (n>9)
        {
            sum[len] = n/10%10;
            len ++;
            n /= 10;
        }
        printf("Case %d:\n", flag++);
        printf("%s + %s = ", a, b);
        for (i=len-1;i>=0;i--)
        {
            printf("%d", sum[i]%10);
        }
        printf("\n");
        if (t)
            printf("\n");
    }
    return 0;
}