题解 | #进制转换#

进制转换

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

幂函数pow

#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;

void trans_to_ten(std::string s){
    int count = 0;
    int len = size(s);
    for(int i = 0; i < len-2; i ++){
        char bit = s[i+2];
        if(bit >= '0' && bit <= '9')
            count += (bit-'0')*pow(16, len-2-i-1);
        else{
            switch (bit) {
                case 'A':count += 10*pow(16, len-2-i-1);break;
                case 'B':count += 11*pow(16, len-2-i-1);break;
                case 'C':count += 12*pow(16, len-2-i-1);break;
                case 'D':count += 13*pow(16, len-2-i-1);break;
                case 'E':count += 14*pow(16, len-2-i-1);break;
                case 'F':count += 15*pow(16, len-2-i-1);break;
                default:break;
            }
            
        }
    }
    cout << count << endl;
}

int main() {

    std::string s;
    while(cin>>s){
        trans_to_ten(s);
    }

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

全部评论

相关推荐

点赞 评论 收藏
分享
点赞 评论 收藏
分享
一表renzha:手写数字识别就是一个作业而已
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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