题解 | #进制转换#

进制转换

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

#include<bits/stdc++.h>
using namespace std;

int hexToDecimal(char c)
{
    if(c>='0'&&c<='9')return c-'0';
    else if(c>='A'&&c<='F')return 10+c-'A';
    else if(c>='a'&&c<='f')return 10+c-'a';
    else return -1;

}

int main() {
    string s;
    cin >> s;
    string ss = s.substr(2);
    int num=0,base=1;
    for(int i = ss.length()-1;i>=0;i--){
        num+=hexToDecimal(ss[i])*base;
        base*=16;
    }
    cout<<num<<endl;
    return 0;
}

全部评论

相关推荐

在看牛客的社畜很积极:身高体重那一行信息去掉,学校那一行的信息放上面,找半天都没找到你是哪个学校什么专业的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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