思路
因为会有可能转化成的数值是超过十进制的,所以先声明一个char类型数组,然后到时候避免出现超过十进制的数字,直接输出对应的数组类型的数字
AC代码
#include<iostream>
#include<string>
#include<stack>
#include<algorithm>
using namespace std;
char num[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
void ans(int a, string n , int b)
{
int res=0,base=1,t=0;
for(int i = n.length()-1; i>=0; i--)
{
t=0;
if(isdigit(n[i])) t=n[i]-'0';
else
{
if(n[i]>='a') t=n[i]-'a'+10;
else t=n[i]-'A'+10;
}
res+=t*base;
base=base*a;
}
stack<char>s;
while(res)
{
s.push(num[res%b]);
res/=b;
}
while(!s.empty())
{
cout<<s.top();
s.pop();
}
cout<<endl;
}
int main()
{
int a, b;
string n;
cin>>a>>n>>b;
ans(a,n,b);
return 0;
}