蓝桥杯练习试题——十六进制转换成八进制




#include <iostream>

#include <string>

#include <math.h>

#include <cmath>


using namespace std;


int main(int argc, const char * argv[]) {

    string hexDecimal;

    cin >> hexDecimal;

    

    int i = 0;

    int length = (int)hexDecimal.size();//最后一个元素的索引值

//    int num = 0;

    int64_t num = 0;

    int temp = 0;

    for (i = 0; i < length; i ++) {

        

        

        if (hexDecimal[i]>='A' && hexDecimal[i]<='F') {

            temp = hexDecimal[i] - 'A' + 10;

        }else{

            temp = hexDecimal[i] - '0';//必须减‘0’,否则会出现意料意外地bug

        }

        num += temp * pow(16, length - i-1);

        

    }

    cout << num;

    return 0;

}


发布了60 篇原创文章 · 获赞 2 · 访问量 3万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章