题解 | #数制转换#
数制转换
https://www.nowcoder.com/practice/8ef02ef8571b417d8c311a87861f7a03?tpId=40&tqId=21387&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan&difficulty=&judgeStatus=&tags=/question-ranking
#include <stdio.h> #include <string.h> #include <math.h> //下述数字其实就是对应位置的ascii码 int main() { char arr[64]; long a, b; while (scanf("%ld %s %ld", &a, arr, &b) != EOF) { int len = strlen(arr); long num=0; //先转化为十进制 for (int i=0;i<len;i++){ int temp = arr[i]; int h; if (temp>=48 && temp<=57){ h = temp-48; }else if(temp >=65 && temp <=70){ h = temp-55; }else { h = temp-87; } long n1 = pow(a, len-i-1)*h; num+=n1; } //再转化为对应进制,取余数的思想 int end=0; int ret[64]={0}; while (num/b) { ret[end++] = (num%b); num/=b; } if (num%b!=0){ ret[end++]=num%b; } //打印,无营养,不用看 for (int i=end-1;i>=0;i--){ if (ret[i]<10){ printf("%d",ret[i]); }else { char cc=ret[i]+55; printf("%c", cc); } } } return 0; }#c##c语言#