题解 | #进制转换2#
进制转换2
https://www.nowcoder.com/practice/ae4b3c4a968745618d65b866002bbd32
#include <iostream> #include<string> #include<vector> using namespace std; //将M进制的数转换为N进制的数输出 //M>=2 N<=36 //本题注意如果进制大于10时,用字符来表示 int chatToInt(char a) { if (a >= 'A' && a <= 'Z') { return a - 'A' + 10; } else { return a - '0'; } } char intToChar(int x) { if (x >= 10) { return x - 10 + 'a'; } else { return x + '0'; } } int main() { int M, N; while (scanf("%d %d", &M, &N) != EOF) { string str; cin >> str; //不要endl long long x = 0; for (int i = 0; i < str.size(); i++) { //str.size() x = (chatToInt(str[i])) * 1 + x * M; } int j = 0; vector<char> answer; while ((int)(x / N) != 0) { char a = intToChar(x % N); answer.push_back(a); x /= N; } answer.push_back(intToChar(x % N)); for (int i = answer.size() - 1; i >= 0; i--) { char b = answer[i]; //直接输出第几个,不要pop了 cout << b; } } } // 64 位输出请用 printf("%lld")