EOJ2568

Sum of digit

Time Limit:1000MS Memory Limit:65536KB
Total Submit:1447 Accepted:778

Description 

Write a program which computes the digit number of sum of two integers a and b.

Input 

The first line of input gives the number of cases, N(1 ≤ N ≤ 100). N test cases follow.
Each test case consists of two integers a and b which are separeted by a space in a line. (0<=a,b<=100000000).

Output 

For each test case, print the number of digits of a + b.

Sample Input 

3
5 7
1 99
1000 999

Sample Output 

2
3
4



下面这个代码是错的,空间用多了,但是好理解。下面会有简化空间复杂度的。


#include 
#include 
#include 
#define N 100000001

int main(){
    char *a, *b, *large, *little;
    int carry = 0, adden1 = 0, adden2 = 0;
    int T = 0, laSize = 0, liSize = 0;
    a = (char *)malloc(sizeof(char) * N);
    b = (char *)malloc(sizeof(char) * N);

    scanf("%d", &T);
    while(T--){
        scanf("%s %s", a, b);
        if(strlen(a) > strlen(b)){
            large = a;
            little = b;
        }else{
            large = b;
            little = a;
        }
        laSize = strlen(large);
        liSize = strlen(little);
        carry = 0;
        while(liSize-- && laSize--){
            carry += large[laSize] + little[liSize] - '0' * 2;
            large[laSize] = carry % 10 + '0';
            carry /= 10;
        }
        while(laSize--){
            carry += large[laSize] - '0';
            large[laSize] = carry % 10 + '0';
            carry /= 10;
        }

        if(carry)
            printf("%d", carry);
        printf("%s\n", large);
    }

    free(a);
    free(b);
    return 0;
}

刚刚傻逼了,看错题了。。。T_T果然是要滚回去读题的节操。。。打了半天又不舍得删掉,放那吧。。。




你可能感兴趣的:(EOJ2568)