题解 | #进制转换#
进制转换
http://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
C代码示例:
#include <stdio.h> #include <string.h> #include <math.h> #define DI 16 int main(){ char str[11]; int i, len, item; while(scanf("%s", str) != EOF){ int num = 0; len = strlen(str)-1; for (i=len; i>1; i--){ switch (str[i]){ case 'A': item = 10; break; case 'B': item = 11; break; case 'C': item = 12; break; case 'D': item = 13; break; case 'E': item = 14; break; case 'F': item = 15; break; default: item = (int)str[i]-48; break; } int tmp = item * (int)pow(DI, len-i); num += tmp; } printf("%d\n", num); } return 0; }