题解 | #进制转换#

进制转换

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

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

int main() {
    string str;
    int res = 0;
    cin >> str;
    string sixteen = "0123456789ABCDEF";
    int ten[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
    for(int i = 2; i < str.length(); i ++){
        for(int j = 0; j < 16; j ++){
            if(str[i] == sixteen[j]){
                res += pow(16, str.length() - i-1) * ten[j];
            }
        }
    }
    cout << res << endl;
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务