这道题比较麻烦,题目理解有点问题,主要是忽略了a,b是用十进制数字1~16表示进制,n是可能用字母(大写或者小写)表示的数字,所以大写字母或者小写字母都要对应处理。
所以这里要用字符串接收n,并逐位处理。
#include<stdio.h>
#include<string.h>
int main()
{
int a,b;
char n[100];
while(scanf("%d%s%d",&a,&n,&b)!=EOF)
{
int A,B;
long y=0;
char ans[100],num=0;
A=a;
B=b;
int count=0,product=1;
while(n[count]!='\0')
{
count++;
}
count--;
//printf("count=%d\n",count);
while(count>=0)
{
if(n[count]<='9'&&n[count]>='0')
y+=(n[count]-'0')*product;
else if(n[count]<='f'&&n[count]>='a')
y+=(n[count]-'a'+10)*product;
else
y+=(n[count]-'A'+10)*product;
count--;
product*=A;
// printf("%d %ld\n",product,y);
}
do
{
if(y%B>9)
ans[num]=(y%B+'A'-10);
else
ans[num]=(y%B+'0');
//printf("ans=%c",ans[num]);
num++;
y/=B;
}while(y!=0);
for(int i=num-1;i>=0;i--)
{
printf("%c",ans[i]);
}
printf("\n");
}
return 0;
}