[刷题] 1022 D进制的A+B (20分)
思路
- 设t = A + B,将每一次t % d的结果保存在int类型的数组s中
- 然后将t / d,直到 t 等于 0为止
- 此时s中保存的就是 t 在 D 进制下每一位的结果的倒序
- 最后倒序输出s数组
#include <iostream>
using namespace std;
int main() {
int a, b, d;
cin >> a >> b >> d;
int t = a + b;
if (t == 0) {
cout << 0;
return 0;
}
int s[100];
int i = 0;
while (t != 0) {
s[i++] = t % d;
t = t / d;
}
for (int j = i - 1; j >= 0; j--)
cout << s[j];
return 0;
}

浙公网安备 33010602011771号