PAT B1022 D进制的A+B

课本AC代码

#include <cstdio>
int main() {
    int a, b, d;
    scanf("%d%d%d", &a, &b, &d);
    int sum = a + b;
    int ans[31], num = 0;
    do {
        ans[num++] = sum % d;
        sum /= d;
    } while(sum != 0);
    for(int i = num - 1; i >= 0; i--) {
        printf("%d", ans[i]);
    }
    return 0;
}

自己写的, 一个错误,找了几个小时找不到

#include <cstdio>
const int max_n = 31;
int main() {
    #ifdef ONLINE_JUDGE
    #else
        freopen("1.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    char c[max_n] = {0};
    long long A = 0, B = 0, D = 0;
    scanf("%lld %lld %lld", &A, &B, &D);
    long long sum = A + B;
    int times = 0;
    //for(int i = 0; sum > D;i++){
    while(sum != 0) {
        long long j = sum % D;
        c[times] = j;
        //printf("%d:%d\n", times, j);
        sum /= D;
        times++;
    }
    c[times] = sum % D;
    //printf("sum:%d times:%d\n", sum, times);
    for(int i = times - 1; i >= 0; i--) {
        printf("%d", c[i]);
    }
    return 0;
}

posted @ 2019-08-04 23:22  哨音  阅读(154)  评论(0编辑  收藏  举报