[D] hex PAT B1022 A + B
Enter two non-negative decimal integers A and B (<= 2 ^ 30-1) , the output A + D B (1 <D <= 10) Number of decimal.
Input format:
Input three integers given sequentially in a row A, B and D.
Output Format:
Output D-nary number of A + B.
Sample input:
1,234,568
Output Sample:
1103
#include <stdio.h>
//十进制转换为D进制(除基取余法)
int main() {
int choose;
printf("输入1 十进制转换为D进制\n输入2 D进制转换为十进制\n");
scanf("%d", &choose);
if (choose == 1) {
int a, d;
printf("输入十进制数 和将要转换的进制D\n");
scanf("%d%d", &a, &d);
int sum = a;
int ans[31], num = 0;
//取余数
do {
ans[num++] = sum % d;
sum = sum / d;
} while (sum != 0);
//逆序输出
for (int i = num - 1; i >= 0; i--) {
printf("%d", ans[i]);
}
} else if (choose == 2) {
int a, d;
printf("输入D进制数 和进制D\n");
scanf("%d%d", &a, &d);
//将D进制的a转换为十进制的y
int y = 0, product = 1;//表示D的0次方
while (a != 0) {
//逐次取出个位
y = y + (a % 10) * product;
//去掉已经计算的个位数
a = a / 10;
//修改权重 d^0 d^1...
product = product * d;
}
printf("%d", y);
}
return 0;
}
Test Results: