题解 | #进制转换#

进制转换

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

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <ostream>
#include <sstream>
#include <string>
using namespace std;

int main() {
   
    string str, real;
    getline(cin, str);
    real = str.substr(2);

    int length = real.length();
    int max_pow = length - 1;
    int trans = 0, c = 0, a = 0;
    for (int i = 0; i <= max_pow; i++) {
   
        switch (real[i]) {
            case 'a':
            case 'b':
            case 'c':
            case 'd':
            case 'e':
            case 'f':
                c = (int)real[i] - 87;
                break;

            case 'A':
            case 'B':
            case 'C':
            case 'D':
            case 'E':
            case 'F':
                c = (int)real[i] - 55;
                break;

            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':

                c = (int)real[i] - 48;

                break;

            default:
                break;
        }

        a = c * pow(16,  max_pow - i);
        trans += a;
        }
    cout << trans;

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

全部评论

相关推荐

10-10 01:10
已编辑
深圳大学 测试开发
面了100年面试不知...:六月到九月,四个项目一个实习,是魔丸吗
投了多少份简历才上岸
点赞 评论 收藏
分享
09-29 15:34
已编辑
北京航空航天大学 C++
做个有文化的流氓:结果是好的,过程不重要,而且你的offer太多了
软开人,秋招你打算投哪些...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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