文章
12
粉丝
693
获赞
3
访问
126.6k
#include<stdio.h>
#include <string.h>
int main() {
int n, b;
char str[10010];
while(scanf("%d%d%s", &n, &b, str) != EOF) {
int len = strlen(str);
long long temp = 0, product = 1;
for(int i = 0; i < len; ++i) {
if(str[i] >= 'a' && str[i] <= 'z') temp = temp + (str[i] - 'a' + 36) * product;
else if(str[i] >= 'A' && str[i] <= 'Z') temp = temp + (str[i] - 'A' + 10) * product;
else temp = temp + (str[i] - '0') * product;
product = product * n;
}
//printf("%lld\n", temp);
char ans[10010];
int i = 0;
if(temp == 0) printf("0\n");
else {
while(temp != 0) {
int x = temp % b;
if(x >= 10 && x <= 35) ans[i++] = (x - 10) + 'A';
else if(x >= 36 && x <= 61) ans[i++] = (x - 36) + 'a';
else ans[i++] = x + '0';
temp = temp / b;
}
for(int j = i - 1; j >= 0; --j)
printf("%c", ans[j]);
printf("\n");
}
}
return 0;
}
登录后发布评论
书上和视频中都讲过这道题哦 https://www.bilibili.com/video/BV1AJ411Y7Fm?p=2