思路:
没什么好说的,按位计算16进制。
代码:
#include<string>
#include<iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;
int main(){
string s;
unordered_map<char,int> hash;
hash['A'] = 10;
hash['B'] = 11;
hash['C'] = 12;
hash['D'] = 13;
hash['E'] = 14;
hash['F'] = 15;
while(cin>>s){
reverse(s.begin(),s.end());
int res = 0;
for(int i = 0,p = 1;i < s.size() - 2; i++,p *= 16){
if(isdigit(s[i])) res += (s[i] - '0') * p;
else{
res += hash[s[i]] * p;
}
}
cout<<res<<endl;
}
return 0;
}
评论区