题解 | #进制转换#
进制转换
http://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
利用哈希表,从输入字符串的最后一位开始计算,计算到输入字符串的第三位
#include<bits/stdc++.h> using namespace std; int main() { unordered_map<char, int> hexmap; hexmap.insert(make_pair('0', 0)); hexmap.insert(make_pair('1', 1)); hexmap.insert(make_pair('2', 2)); hexmap.insert(make_pair('3', 3)); hexmap.insert(make_pair('4', 4)); hexmap.insert(make_pair('5', 5)); hexmap.insert(make_pair('6', 6)); hexmap.insert(make_pair('7', 7)); hexmap.insert(make_pair('8', 8)); hexmap.insert(make_pair('9', 9)); hexmap.insert(make_pair('A', 10)); hexmap.insert(make_pair('B', 11)); hexmap.insert(make_pair('C', 12)); hexmap.insert(make_pair('D', 13)); hexmap.insert(make_pair('E', 14)); hexmap.insert(make_pair('F', 15)); string str; cin>>str; int len=str.size(); int num=0; for(int i=len-1;i>1;i--) { num+=hexmap.find(str[i])->second*pow(16.0, (double)len-i-1); } cout<<num<<endl; return 0; }