题解 | #数制转换#
数制转换
https://www.nowcoder.com/practice/8ef02ef8571b417d8c311a87861f7a03
#include <stdio.h> #include <string.h> int main() { int a, b; char n[105]; while (scanf("%d %s %d", &a, n, &b) != EOF) { int len = strlen(n); int ans = 0; for (int i = 0; i < len; i++) { ans *= a; if (n[i] >= '0' && n[i] <= '9') { ans += (n[i] - '0'); } else if (n[i] >= 'a' && n[i] <= 'f') { ans += (n[i] - 'a') + 10; } else if (n[i] >= 'A' && n[i] <= 'F') { ans += (n[i] - 'A') + 10; } } char out[105]; int cnt = 0; while (ans > 0) { int w = ans % b; if (w < 10) { out[cnt++] = w + '0'; } else { out[cnt++] = (w - 10) + 'A'; } ans /= b; } for (int i = cnt - 1; i >= 0; i--) { printf("%c", out[i]); } printf("\n"); } return 0; }