求解:为什么高精加代码过不了这道题

P1001 A+B Problem

極灬青龙 @ 2019-10-02 10:10:08

以下为代码

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

int main() {
    char a[501] = {'\0'},b[501] = {'\0'};
    int c[502] = {0},cd = 0;
    scanf("%s%s",&a,&b);
    for(int i = 501;i > 0;i--){
        a[i] = a[i - 1];
    }
    for(int i = 501;i > 0;i--){
        b[i] = b[i - 1];
    }
    a[0] = '0';
    b[0] = '0';
    c[0] = 0;
    int A = strlen(a) - 1;
    int B = strlen(b) - 1;
    if(A > B){
        for(int i = A;i > A - B;i--){
            c[i] += b[i - A + B] - '0';
        }
        for(int i = A;i >= 0;i--){
            c[i] += a[i] - '0';
            if(cd == 1){
                cd--;
                c[i] += 1;
            }
            if(c[i] >= 10){
                cd = 1;
                c[i] -= 10;
            }
        }
    }else if(B > A){
        for(int i = B;i > B - A;i--){
            c[i] += a[i - B + A] - '0';
        }
        for(int i = B;i >= 0;i--){
            c[i] += b[i] - '0';
            if(cd == 1){
                cd--;
                c[i] += 1;
            }
            if(c[i] >= 10){
                cd = 1;
                c[i] -= 10;
            }
        }

    }else{
        for(int i = A;i >= 0;i--){
            c[i] = ( a[i] - '0' ) + ( b[i]  - '0' );
            if(cd != 0){
                cd = 0;
                c[i] += 1;
            }
            if(c[i] >= 10){
                cd = 1;
                c[i] -= 10;
            }
        }
    }
    if(A >= B){
        for(int i = 0;i <= A;i++){
            if(c[i] == 0 && i == 0){
                continue;
            }
            printf("%d",c[i]);
        }
    }else{
        for(int i = 0;i <= B;i++){
            if(c[i] == 0 && i == 0){
                continue;
            }
            printf("%d",c[i]);
        }
    }
    return 0;

by _gifbmp @ 2019-10-02 10:10:37

@中国魂 %s不能加&


by 極灬青龙 @ 2019-10-02 10:10:43

过不了P1001


by 極灬青龙 @ 2019-10-02 10:12:16

改了以后还是过不去


by 142857cs @ 2019-10-02 10:14:43

有负数


by Sya_Resory @ 2019-10-02 10:23:17

@中国魂 P1001你搞个鬼高精啊


by Lucky_Xiang @ 2019-10-02 10:34:23

@中国魂 有负数


by 極灬青龙 @ 2019-10-02 10:59:52

感謝各位大佬鼎力相助


by legendgod @ 2019-10-06 16:08:12

@中国魂

1.少了一个括号在末尾

2.负数的加法有问题


by legendgod @ 2019-10-06 16:08:56

例如:1+(-1)=-32

你的代码


|