1022. D进制的A+B (20)
原题: https://www.patest.cn/contests/pat-b-practise/1022
思路: 10进制转d进制, d的范围[2, 10], 与10进制转2进制操作方法一样, 注意0转成任何进制
还是0
完整C语言实现:
#include <stdio.h>
int main () {
int a;
int b;
int n;
int d;
int len = 40;
char res[len];
int start = len;
int i;
scanf("%d %d %d", &a, &b, &d);
n = a + b;
if (n == 0) {
printf("0\n"); // 对应测试点3
} else {
while (n != 0) {
start--;
res[start] = (n % d) + '0';
n = n / d;
}
for (i=start; i<len; i++) {
putchar(res[i]);
}
printf("\n");
}
return 0;
}