环境:centos6.5, g++
程序输入:十进制源数字 目的进制
程序输出:源数字的目的进制表示
代码:
<span style="font-family:Times New Roman;font-size:24px;">
#include <iostream>
#include <string>
using namespace std;
int main(){
unsigned int source = 0;
unsigned int index = 0;
cin >> source >> index;
char *data = new char[20];
char *pbegin = data;
char *ptail = data;
char *pstart = data;
char *pend = data;
while(source/index){
*pend++ = source%index + '0';
source = source/index;
}
*pend = source + '0';
ptail = pend+1;
while(pstart < pend){
char c = *pstart;
*pstart = *pend;
*pend = c;
++pstart;
--pend;
}
string str(pbegin,ptail);
cout << str << endl;
delete [] pbegin;
return 0;
}</span>
注:如果目标进制超过10,比如16进制,将【source%index+'0'】改为【"0123456789ABCDEF"[source%index]】即可