题解 | #数制转换#
数制转换
https://www.nowcoder.com/practice/8ef02ef8571b417d8c311a87861f7a03
决定了,以后遇上进制转换就这么写!
#include<cstdio> #include<string> #include<vector> using namespace std; int charToint(char num) { if (num >= '0' && num <= '9') { return num - '0'; } else if (num >= 'a' && num <= 'z') { return num + 10 - 'a'; } else if (num >= 'A' && num <= 'Z') { return num + 10 - 'A'; } return 0; } char intTochar(int num) { if (num > 9) { return num - 10 + 'A'; } else { return num + '0'; } } int main() { int a, b; char n[1001]; while (scanf("%d%s%d", &a, n, &b) != EOF) { string str = n; long long int res = 0; for (int i = 0; i < str.size(); i++) {//x进制转十进制 res *= a; res += charToint(str[i]); } vector<char> ans; while (res != 0) {//十进制转x进制 ans.push_back(intTochar(res % b)); res /= b; } for (int i = ans.size() - 1; i >= 0; i--) { printf("%c", ans[i]); } printf("\n"); } return 0; }