应该是比较容易看懂的,思路清晰,我是第一
进制转换2
https://www.nowcoder.com/practice/ae4b3c4a968745618d65b866002bbd32
#include<iostream> #include<string> #include<algorithm> #include<stack> #include<math.h> using namespace std; int main() { int M, N; string str; cin >> M >> N; cin >> str;//输入 if (str == "0") { cout << "0"; return 0; }//特殊情况 long long int ten = 0;//M进制转10进制暂存至ten,会超出int范围,用long stack<int> res;//10进制转N进制暂存到res栈 for (int i = 0; i < str.length(); i++) {//M进制转10进制 if (str[i] >= '0' && str[i] <= '9') ten += (str[i] - '0') * pow(M, str.length() - i - 1); else ten += (str[i] - 'A' + 10) * pow(M, str.length() - i - 1); } while (ten != 0) {//10进制转N进制 res.push(ten % N); ten /= N; } while (!res.empty()) {//出栈即为结果 if (res.top() <= 9) cout << res.top(); else printf("%c", res.top() - 10 + 'a'); res.pop(); } }