书接上回,我们可以用10进制作为过渡,把a进制转换成b进制
//a进制转换为b进制
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
char s1[100];
cin>>s1;
//1.将a进制的数转化为十进制
int sum=0;
for(int i=0;i<strlen(s1);++i){
int temp;
if(s1[i]>='0' && s1[i]<='9') //数字字符转化为相对应的数字
temp=s1[i]-'0';
else if(s1[i]>='A' && s1[i]<='Z')
temp=s1[i]-'A'+10; //大写字母转化为相对应的数字 A===10 B===11......
sum+=temp*pow(a,strlen(s1)-1-i);
}
//2.将十进制数转化为b进制
char s2[100];
int idx=0;
while(sum>0){
char temp2;
if(sum%b>=0 && sum%b<=9) //余数如果是0-9,就转化为对应的数字字符
temp2=sum%b+'0';
else
temp2=sum%b-10+'A'; //余数超过10转化为相对应的大写字母
s2[