#include <cmath> #include <cstdint> #include <iostream> #include <unordered_map> using namespace std; int main() { string s; unordered_map<char,int> umap={{'0',0},{'1',1},{'2',2},{'3',3},{'4',4},{'5',5},{'6',6},{'7',7}, {'8',8},{'9',9},{'A',10},{'B',11},{'C',12},{'D',13},{'E',14},{'F',15}}; while (cin>> s) { // 注意 while 处理多个 case int n=s.length(); long long ans=0; for(int i=2;i<n;i++){ ans+=pow(16, n-i-1)*umap[s[i]]; } cout << ans << endl; } } // 64 位输出请用 printf("%lld")
使用哈希表查找字符对应数字,简化代码