题解 | #进制转换#

进制转换

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

#include <iostream>
#include <cmath>
#include <string>
using namespace std;
//解题思路:逆序遍历字符串,构建映射关系:A-10、B-11、C-12、D-13、E-14、F-15
int main() {
    string str;
    while(cin>>str){
        int N=str.size();
        int number=0;
        int count = 0;
        for(int i=N-1; i>=2; i--,count++){
            int temp;
            if(str[i]>64){//A~F与真实十六进制值差了55
                temp = str[i]-55;
            }else{//正常的0~9的数字
                temp = stoi(str.substr(i,1));
            }
            number += temp*pow(16,count);
        }
        cout<<number<<endl;
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

Lorn的意义:你这种岗位在中国现在要么牛马天天加班,要么关系户进去好吃好喝,8年时间,真的天翻地覆了,对于资本来说你就说一头体力更好的牛马,哎,退伍没有包分配你真的亏了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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