题解 | #进制转换2#
进制转换2
http://www.nowcoder.com/practice/ae4b3c4a968745618d65b866002bbd32
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main()
{
int m, n;
cin >> m >> n;
long long int value = 0;
string str;
cin >> str;
//转为十进制
for (int i = 0; i < str.size(); ++i) {
int current = (str[i] >= '0' &&
str[i] <= '9') ? int(str[i] - '0') :
int(str[i] - 'A' + 10);
value = value * m + current;
}
//转为n进制
stack<char> ans;
while (value) {
ans.push(value % n < 10 ? char((value % n) + '0') :
char((value % n) - 10 + 'a'));
value /= n;
}
while (!ans.empty()) {
cout << ans.top();
ans.pop();
}
}