//一个int型数a(建设为5),a+'0'为53,所以此时若想表示‘5’,则应用char强制类型转换,若用to_string则为”53“ #include "stdio.h" #include "string" #include "algorithm" using namespace std; string change(int init,int ter,string numStr){//init为原先的进制,ter为转换后的进制 string str="";//记录转换进制后的字符串 //num[]为要转换的字符换 int len = numStr.size(); for (int i = 0; i < len; ) { int remainder = 0;//每次除法时,余数肯定默认是0 for (int j = i; j < len; ++j) {//j从i开始是为了抹掉开始的0 int k = (remainder*init + numStr[j]-'0')%ter; numStr[j] = (remainder*init + numStr[j]-'0')/ter + '0'; remainder = k; } str = str+char (remainder+'0'); while (numStr[i]=='0') ++i; } return str; } int main(){ char inPUT[1010]; while (scanf("%s",inPUT)!=EOF){ string binary = change(10,2,inPUT); string decimal = change(2,10,binary); reverse(decimal.begin(),decimal.end()); printf("%s",decimal.c_str()); } }