题解 | 进制转换

进制转换

https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6

#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")

使用哈希表查找字符对应数字,简化代码

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-24 13:35
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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