題目地址: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; }