AcWing 3452. 进制转换
原题链接
简单
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 1e6 + 10;
int t, n, m, k, l, r, op, x, y;
string str;
bool flag ;
void solve() {
while (cin >> str) {
flag = false;
if (str[0] == '-') {
str.erase(str.begin());
flag = true;
}
str = "~" + str.substr(2);
int siz = str.size() - 1;
int ans = 0;
for (int i = 1; i <= siz; i++) {
if(str[i]>='0'&&str[i]<='9'){
k=str[i]-'0';
}else if(str[i]>='A'&&str[i]<='Z'){
k=str[i]-'A'+10;
}else{
k=str[i]-'a'+10;
}
ans += k * pow(16, siz - i);
}
if (flag)cout << "-";
cout << ans << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}