C++ 实现进制转换:将 n 进制数转换为 m 进制数
#include
using namespace std;
int main() { int n, m; string num; cin >> n >> num >> m;
// 将 n 进制数转化为十进制数
int dec = 0;
for (int i = 0; i < num.size(); i++)
{
int x = isdigit(num[i]) ? num[i] - '0' : num[i] - 'A' + 10;
dec = dec * n + x;
}
// 将十进制数转化为 m 进制数
string res;
while (dec)
{
int x = dec % m;
res += (x < 10 ? x + '0' : x - 10 + 'A');
dec /= m;
}
reverse(res.begin(), res.end());
cout << res << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/oSmo 著作权归作者所有。请勿转载和采集!