题解 | #进制转换#

进制转换

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

#include <iostream>
#include <string>
#include <map>
#include <cmath>
using namespace std;
const array<char, 16> ss = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int main() {
    map<char, int> mm;
    for (int i =0; i < ss.size(); ++i)
    {
        mm[ss[i]] = i;
    }
    string input;
    cin >> input;

    int i = 0;
    int res = 0;
    string::reverse_iterator backItor = input.rbegin();
    for (; backItor < input.rend(); backItor++)
    {
        char s = *backItor;
        if (s == 'x'|| s == 'X')
        {
            break;
        }

        //2
        
        res += pow(16, i) * mm[s]; 
        ++i;
    }
    cout << res;
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-27 10:46
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务