题解 | #进制转换#

进制转换

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

#include <cmath>
#include <iostream>
#include <string>
#include "string"
using namespace std;

int hex2dec(char ch){
    if (ch == 'A') {
        return 10;
    }else if (ch == 'B') {
        return 11;
    }else if (ch == 'C') {
        return 12;
    }else if (ch == 'D') {
        return 13;
    }else if (ch == 'E') {
        return 14;
    }else if (ch == 'F') {
        return 15;
    }
    return ch-'0';
}

int main() {
    string s;
    cin>>s;
    s=s.substr(2,s.size()-2);
    int i = 0, out=0;
    for (i=0; i<s.size(); i++) {
        out+=hex2dec(s[i])*pow(16, s.size()-1-i);
    }
    cout<<out;

}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

牛至超人:把哈工大,再加大加粗,看见闪闪发光的哈工大字样,面试官直接流口水
投递字节跳动等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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