生疏了真生疏了
// #include <cstdio> // #include <string> // #include <vector> // using namespace std; // int CharToInt(char c) { // if (c >= '0' && c <= '9') { // return c - '0'; // } // else if (c >= 'a' && c <= 'z') { // return c - 'a' + 10; // } // else { // return c - 'A' + 10; // } // } // char IntToChar(int x) { // if (x < 10) { // return x + '0'; // } // else { // return x - 10 + 'a'; // } // } // int main() { // // m表示原来是m进制数,要转成n进制数 // int m, n; // char buf[100]; // while (scanf("%d%s%d", &m, buf, &n) != EOF) { // string str = buf; // long long number = 0; // for (unsigned i = 0; i < str.size(); ++i) { // number *= m; // number += CharToInt(str[i]); // } // vector<char> answer; // if (number == 0) { // answer.push_back('0'); // } // else { // while (number != 0) { // answer.push_back(IntToChar(number % n)); // number /= n; // } // } // for (int i = answer.size() - 1; i >= 0; --i) { // // ASCII表中小写字母比大写字母大32 // if (answer[i] >= 'a' && answer[i] <= 'z') { // answer[i] -= 32; // } // // if (answer[i] >= 'A' && answer[i] <= 'Z') { // // answer[i] += 32; // // } // printf("%c", answer[i]); // } // printf("\n"); // } // return 0; // } #include <string> #include <iostream> #include <map> #include <cmath> #include <vector> using namespace std; int main() { int before, after; string s; while (cin >> before >> s >> after) { map<char, int> CTOI = { {'a', 10}, {'b', 11}, {'c', 12}, {'d', 13}, {'e', 14}, {'f', 15} }; map<int, char> ITOC = { {10, 'A'}, {11, 'B'}, {12, 'C'}, {13, 'D'}, {14, 'E'}, {15, 'F'} }; long long ans = 0; // 去除前导0 int pos = 0; while (s[pos] == '0') { pos++; } string str = s.substr(pos); int cnt = 0; for (int i = str.size() - 1; i >= 0; i--, cnt++) { if (str[i] >= 'A' && str[i] <= 'F') { str[i] += 32; // 统一转换为小写处理 ans += CTOI[str[i]] * pow(before, cnt); continue; } else if (str[i] >= 'a' && str[i] <= 'f') { ans += CTOI[str[i]] * pow(before, cnt); continue; } else { ans += (str[i] - '0') * pow(before, cnt); } } // 再转为目标进制 vector<int> digits; while (ans) { digits.push_back(ans % after); ans /= after; } for (auto it = digits.rbegin(); it != digits.rend(); it++) { int num = *it; if (num >= 10) { cout << ITOC[num]; // 全部按大写输出 continue; } cout << num; } cout << endl; } return 0; }