1002

杭电OJ
A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 146895    Accepted Submission(s): 27722


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






//这是大数运算的题。

解法如下:


/*
	Name: 大数加法 
	Copyright: 
	Author: skywolf
	Date: 23-03-13
	Description: 本文为原创,转载请注明出处。
*/

#include <stdio.h>
#include <string.h>

int main()
{
    int T, i, j, k, p;
    int len_a, len_b;
    char A[1001], B[1001], t[1001];
    int C[1001];

    scanf("%d", &T);

    for(p=0; p<T; p++)
    {   
	if(p) printf("\n");
        len_a = len_b = 0;
        memset(C, 0, sizeof(C));
        memset(t, '\0', sizeof(t));
        scanf("%s%s", A, B);
        printf("Case %d:\n%s + %s = ", p+1, A, B);
        /**获取字符串长度。*/
        for(i=0; A[i]!='\0'; i++);
        len_a = i;
        for(i=0; B[i]!='\0'; i++);
        len_b = i;


        if(len_a < len_b)
        {
            strcpy(t, A);
            memset(A, '\0', sizeof(A));
            strcpy(A, B);
            memset(B, '\0', sizeof(B));
            strcpy(B, t);
            i = len_a;
            len_a = len_b;
            len_b = i;											             
            /*字符串交换后,长度也要交换。*/
            /*printf("%s %s\n", A, B);*/
        }
        /**判断是否要进位*/
        int flag = 0;
        int len_c = len_a;
        k = len_a;
        j = len_b;
        for(i=len_b-1; i>=0; i--)
        {

            C[--len_c] = A[--len_a] + B[i] - '0' - '0';
            if(flag)
            {
                C[len_c]++;
                flag--;
            }
            if(C[len_c] > 9)
            {
                flag++;
                C[len_c] -= 10;
            }
        }
        if(k == j && flag)
        {
            printf("1");
        }
        for(i=len_c-1; i>=0; i--)
        {
            C[i] = A[i]  - '0' + flag;
            if(flag)
            {
                flag--;
            }
            if(C[i] > 9)
            {
            /*如果遍历到最后一位,直接 不用管了。等下直接输出就行了。*/				
                if(0 == i)   
                {
                    break;
                }
                C[i] -= 10;
                flag++;
            }
        }
        for(i=0; i<k; i++)
        {
            printf("%d", C[i]);
        }
        printf("\n");
    }
    return 0;
}











你可能感兴趣的:(OJ)