#include<iostream>
#include<string>
using namespace std;
int main()
{
int n, r;
while (cin >> n >> r)
{
string s;
int sym = 0;
if (n < 0)
{
sym = 1;
n = -n;
}
if (r > 10)
while (n != 0)
{
if (n%r >= 10)//刚开始这里没加等号WA,楞看了半个多小时才看出来
s += n%r - 10 + 'A';
else s += n%r + '0';
n /= r;
}
else while (n != 0)
{
s += n%r + '0';
n /= r;
}
reverse(s.begin(), s.end());
if (sym == 1)
cout << '-' << s << endl;
else cout << s << endl;
}
return 0;
}
抓住进制转化的模式即可,巧妙用string进行操作
2031 进制转换
最新推荐文章于 2022-06-14 10:26:01 发布