题解 | #进制转换2#
进制转换2
https://www.nowcoder.com/practice/ae4b3c4a968745618d65b866002bbd32
#include <stdio.h> #include<math.h> // 计算字符串长度: int Length(char a[]) { int n = 0; for (int i = 0; a[i] != '\0'; i++) { n++; } return n; } int main() { int m,n; scanf("%d %d",&m,&n); char a[10]=" "; scanf("%s",a); int len=Length(a); long number=0; int i=0; while(len--) { int l=0; if(a[len]>='A') l=10+(a[len]-'A'); else l=a[len]-'0'; number+=l*(pow(m,i)); i++; }//转化为十进制数 char b[10]=" "; char ch; int c=0; int j=0; while(number!=0) { c=number%n; if(c>=10) ch='a'+(c-10); ch='0'+c; b[j++]=ch; number/=n; } char d[10]=" "; int num=Length(b); int k=0; while(num--) { d[k++]=b[num]; } printf("%s",d); return 0; }