提交时间:2024-08-19 22:40:27

运行 ID: 168103

#include<bits/stdc++.h> using namespace std; string convertBase(int fromBase, int toBase, string number) { int decimalNumber = 0; for (char digit : number) { decimalNumber *= fromBase; decimalNumber += digit >= '0' && digit <= '9'? digit - '0' : digit - 'A' + 10; } string result; while (decimalNumber > 0) { int remainder = decimalNumber % toBase; char digit = remainder < 10? remainder + '0' : remainder - 10 + 'A'; result = digit + result; decimalNumber /= toBase; } return result.empty()? "0" : result; } int main() { int fromBase, toBase; string number; while (cin >> fromBase >> number >> toBase) { cout << convertBase(fromBase, toBase, number) << endl; } return 0; }