题解 | #进制转换2# 模板
进制转换2
https://www.nowcoder.com/practice/ae4b3c4a968745618d65b866002bbd32
#include <bits/stdc++.h>
using namespace std;
string convert(string s, int m, int b) {
string ans;
while (!s.empty()) {
int k = 0;
bool leadingZero = true;
string temp;
for (int i = 0; i < s.size(); i++) {
int digit = (isdigit(s[i]) ? s[i] - '0' : s[i] - 'A' + 10);
int t = (k * m + digit) % b;
int quotient = (k * m + digit) / b;
k = t;
if (quotient != 0 || !leadingZero) {
temp += (quotient < 10 ? quotient + '0' : quotient - 10 + 'A');
leadingZero = false;
}
}
ans += (k < 10 ? k + '0' : k - 10 + 'A');
s = temp;
}
reverse(ans.begin(), ans.end());
return ans.empty() ? "0" : ans;
}
int main(){
int m,n;
string s;
cin >> m >> n;
cin >> s;
string answer = convert(s,m,n);
cout << answer << endl;
return 0;
}

查看23道真题和解析