进制转换2
进制转换2
http://www.nowcoder.com/questionTerminal/ae4b3c4a968745618d65b866002bbd32
#include<stdio.h> #include<string.h> #include<ctype.h> #include<math.h> int M,N; char ans[100]; void MtoN(char *num) { int len = strlen(num); long long decimal = 0; for(int i = len-1;i>=0;i--) // M转为10进制 { if (isupper(num[i])) decimal += (num[i] - 'A'+10) * (long long) pow(M, len - 1 - i); else decimal += (num[i] - '0') * (long long) pow(M, len - 1 - i); } int j = 0; long long temp; while(decimal) //10进制转N进制 { temp = decimal % N; if(temp < 10) ans[j++] = temp+'0'; else ans[j++] = temp - 10 + 'a'; decimal /= N; } for(int i = j-1;i>=0;i--) printf("%c",ans[i]); return; } int main() { scanf("%d %d",&M,&N); char num[100]; scanf("%s",num); MtoN(num); return 0; }