讲解
转化成n进制就对n取余,如果这个余数大于9就要转换成英文字母,用string存储余数(因为最后要倒序输出),取余了最后要除n,一直循环直到num==0
,然后反转字符串。
类似于十进制转换成二进制,就是注意
余数大于9就要转换成英文字母。
源码
string tento(int num, int n)
{
string res;
while(num)
{
int tmp = num % n;
string s;
if (tmp >= 10) s = 'A' + tmp - 10;
else s = to_string(tmp);
res += s;
num /= n;
}
reverse(res.begin(), res.end());
return res;
}