//a-f|A-F用数组下标解决,这个想法很好 #include "stdio.h" #include "string" #include "math.h" #include "algorithm" using namespace std; int main(){ int num1,num3;//num1为原进制,num3为转换后的进制 char num2[30]; string change = "0123456789ABCDEF";//eg:A的下标为10,所以A代表10 scanf("%d %s %d",&num1,num2,&num3); string str = num2; for (int i = 0; i < str.size(); ++i) { if (str[i] >= 'a' && str[i] <= 'z') str[i] -= 32; } int sum = 0; for (int i = str.size()-1; i >= 0; --i) {//计算出对应的十进制 for (int j = 0; j < change.size(); ++j) { if (change[j] == str[i]){//change的下标j即为当前位的数字 sum += j*pow(num1,str.size()-i-1); break; } } } string End = ""; int remainder; while (sum != 0){ remainder = sum%num3; sum = sum/num3; End += change[remainder]; } reverse(End.begin(),End.end()); printf("%s",End.c_str()); }