原题地址
https://www.luogu.com.cn/problem/P1143
解题思路
我是老老实实先转为十进制然后再转的~
参考代码
#include <bits/stdc++.h>
using namespace std;
string st = "ABCDEF";
int main() {
int n, m;
string nn;
cin >> n >> nn >> m;
int y = 0, k = 0;
for (auto i = nn.rbegin(); i != nn.rend(); ++i) {
int num;
if (isdigit(*i)) num = *i - '0';
else num = *i - 'A' + 10;
y += num * pow(n, k);
k++;
}
//cout << y << endl;
string ans = "";
do {
int left = y % m;
if (left >= 10) ans += char('A' + left - 10);
else ans += to_string(left);
y /= m;
} while (y);
for (auto it = ans.rbegin(); it != ans.rend(); ++it) cout << *it;
return 0;
}