最笨办法

进制转换2

https://www.nowcoder.com/practice/ae4b3c4a968745618d65b866002bbd32

#include <stdio.h>
#include<string.h>
int main(){
int M,N;char X[32];
scanf("%d%d%s",&M,&N,X);
int a[32],k=0;
for(int i=0;i<strlen(X);i++){
	if(X[i]=='A')a[k++]=10;
	if(X[i]=='B')a[k++]=11;
	if(X[i]=='C')a[k++]=12;
	if(X[i]=='D')a[k++]=13;
	if(X[i]=='E')a[k++]=14;
	if(X[i]=='F')a[k++]=15;
	if(X[i]=='G')a[k++]=16;
	if(X[i]=='H')a[k++]=17;
	if(X[i]=='I')a[k++]=18;
	if(X[i]=='J')a[k++]=19;
	if(X[i]=='K')a[k++]=20;
	if(X[i]=='L')a[k++]=21;
	if(X[i]=='M')a[k++]=22;
	if(X[i]=='N')a[k++]=23;
	if(X[i]=='O')a[k++]=24;
	if(X[i]=='P')a[k++]=25;
	if(X[i]=='Q')a[k++]=26;
	if(X[i]=='R')a[k++]=27;
	if(X[i]=='S')a[k++]=28;
	if(X[i]=='T')a[k++]=29;
	if(X[i]=='U')a[k++]=30;
	if(X[i]=='V')a[k++]=31;
	if(X[i]=='W')a[k++]=32;
	if(X[i]=='X')a[k++]=33;
	if(X[i]=='Y')a[k++]=34;
	if(X[i]=='Z')a[k++]=35;
	if(X[i]>='0'&&X[i]<='9')a[k++]=X[i]-'0';
}
long long sum=0,q=0;
for(int i=k-1;i>=0;i--){
	sum=sum+a[i]*pow(M,q++);
}

int i=0;
while(sum){
	a[i++]=sum%N;
	sum/=N;
}
for(int j=i-1;j>=0;j--)
	printf("%d",a[j]);
	return 0;
}

全部评论

相关推荐

无情咸鱼王的秋招日记之薛定谔的Offer:好拒信,偷了,希望有机会用到
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务