经典指数          
原因
1434
浏览数
0
收藏数
 

编一个程序,它能读入两个正整数(第一个:n进制的基数(2-19)第二个:要转换的十进制数),并输出这个正整数的2-19进制形式,依次用A、B、C、D、E、F、G、H、I表示10、11、12、13、14、 15、16、17、18。如输入为15 56时,输出为3B。

     举报   纠错  
 
切换
1 个答案

string convert(unsigned int base,unsigned int num)

{

    if(base<2||base>19)

        return NULL;

    string ans;

    while(num)

    {

        unsigned int yushu=num%base;

        char c=yushu<10?yushu+'0':yushu-10+'A';

        ans+=c;

        num=num/base;

    }

    reverse(ans.begin(),ans.end());

    return ans.size()==0?"0":ans;

}

 
切换
撰写答案