PAT B1002 D进制的A+B
Posted ischenjy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT B1002 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;
以上是关于PAT B1002 D进制的A+B的主要内容,如果未能解决你的问题,请参考以下文章