进制转换
例题(P1143 进制转换)
思路:由 \(n\) 进制转为 \(10\) 进制,再转为 \(m\) 进制。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n,m;
string s;
LL cnt;
LL o = 0;
string c[20] = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
LL bow(int d,int r){
LL num = 1;
for(int i = 1;i <= r;i++) num *= d;
return num;
}
string tmp(LL o,LL m){
string as = "";
while(o > 0){
int mod = o % m;
o /= m;
as.append(c[mod]);
}
return as;
}
int main(){
scanf("%lld", &n);
cin>>s;
scanf("%lld", &m);
cnt = 0;
for(int i = s.length() - 1;i >= 0;i--){
LL bbb = bow(n , cnt);
if(s[i] <= '9' && s[i] >= '0') o +=1ll * bbb * (int(s[i]) - 48);
else o += 1ll* bbb * (int(s[i]) - 55);
cnt++;
}
//o -> 10 JZ
string aa = tmp(o,m);
for(int i = aa.length() - 1;i >= 0;i--){
cout<<aa[i];
}
return 0;
}
意义自明,不讲述。