题解 | #进制转换# C++ 哈希表解决

进制转换

http://www.nowcoder.com/questionTerminal/8f3df50d2b9043208c5eed283d1d4da6

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <unordered_map>

using namespace std;

const int N = 100010;

unordered_map<char, int> tab={{'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}};

int main() {
    string str;
    while (cin >> str&&!str.empty()) { // 注意 while 处理多个 case
        int ans = 0;
        for(int i = 0 ;i<str.size();++i)
            ans = ans *16 + tab[tolower(str[i])];
        cout << ans;
    }
}
// 64 位输出请用 printf("%lld")
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务