题解 | #数制转换#
数制转换
http://www.nowcoder.com/practice/8ef02ef8571b417d8c311a87861f7a03
很丑但是很直观的解法
#include <iostream>
#include <vector>
using namespace std;
vector<int> res;
int main()
{
string n;
int a, b;
cin >> a >> n >> b;
long long x = 0;
while (n.size() > 1 && n.front() == '0') //去除前导0
n = n.substr(1);
for (int i = 0; i < n.size(); ++i) //转成十进制了
if (n[i] >= '0' && n[i] <= '9')
x = x * a + n[i] - '0';
else if (n[i] >= 'a' && n[i] <= 'f')
x = x * a + n[i] - 'a' + 10;
else if (n[i] >= 'A' && n[i] <= 'F')
x = x * a + n[i] - 'A' + 10;
while (x) //除留余数
{
res.push_back(x % b);
x /= b;
}
for (int i = res.size() - 1; i >= 0; --i)
{
if (res[i] >= 10)
printf("%c", 'A' + res[i] - 10);
else
cout << res[i];
}
return 0;
}