Problem | Verdict | Lang | Time | Best | Rank | Submit Time |
---|---|---|---|---|---|---|
| discuss389 - | Accepted | C++11 | 0.060 | 0.000 | 465 | 4 mins ago |
Suggest:
- Convert BaseA to Decimal
- Convert Decimal to BaseB
#include<bits/stdc++.h>
#define int long long
using namespace std;
string s;
int a, b;
int f(char x){
if (x=='A') return 10;
if (x=='B') return 11;
if (x=='C') return 12;
if (x=='D') return 13;
if (x=='E') return 14;
if (x=='F') return 15;
return x-'0';
}
char ff(int n){
if (n<10) return char('0'+n);
return char('A'+n-10);
}
int toDec(string s, int a){
int hat= 0, res= 0;
for(int i=s.size()-1; i>=0; i--)
res += f(s[i])*pow(a, hat++);
return res;
}
string toAns(int n, int b){
string s="";
while(1){
s = ff(n%b) + s;
n /= b;
if (n==0) break;
}
return s;
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0);
while(cin >> s >> a >> b){
int dec= toDec(s, a);
string ans= toAns(dec, b);
if (ans.size() > 7)
cout << setw(7) << "ERROR"<< "\n";
else
cout << setw(7) << ans << "\n";
}
}
No comments:
Post a Comment