题解 | 数制转换
数制转换
https://www.nowcoder.com/practice/8ef02ef8571b417d8c311a87861f7a03
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main() {
int a, b;
string str, res;
long temp;
int count;
int t;
while (cin >> a >> str >> b) {
temp = 0;
count = 0;
for (int i = str.size() - 1; i >= 0; i--) {
if (str[i] >= 'A' && str[i] <= 'Z') {
t = str[i] - 'A' + 10;
} else if (str[i] >= 'a' && str[i] <= 'z') {
t = str[i] - 'a' + 10;
} else {
t = str[i] - '0';
}
temp += t * pow(a, count);
count++;
}
count = 0;
int i = 0;
while (temp) {
t = temp % b;
temp /= b;
if (t > 9) {
switch (t) {
case 10:
res.push_back('A');
break;
case 11:
res.push_back('B');
break;
case 12:
res.push_back('C');
break;
case 13:
res.push_back('D');
break;
case 14:
res.push_back('E');
break;
case 15:
res.push_back('F');
break;
}
} else {
res.push_back(t + '0');
}
}
for (int j = res.size() - 1; j >= 0; j--) {
cout << res[j];
}
}
}
// 64 位输出请用 printf("%lld")
查看7道真题和解析