题目地址:https://pintia.cn/problem-sets/994805260223102976/problems/994805299301433344ios
输入两个非负 10 进制整数 A 和 B \((≤2^{30−1})\),输出 A+B 的 D (1<D≤10)进制数。数组
输入在一行中依次给出 3 个整数 A、B 和 D。spa
输出 A+B 的 D 进制数。code
123 456 8
1103
进制转换问题,将10进制数转化为D进制数,使用栈依次存储10进制数对D的余数,直到商为0为止。而后出栈便可。可贵的一遍AC、、、ci
#include <iostream> #include <stack> using namespace std; int main() { long long A, B; short D; cin >> A >> B >> D; long long remainder = (A + B) % D; long long C = (A + B) / D; stack<long long> s; // 先进行求余,再计算商 while (C != 0) { s.push(remainder); remainder = C % D; C /= D; } s.push(remainder); while (!s.empty()) { cout << s.top(); s.pop(); } cout << endl; return 0; }