藍橋杯練習試題——十六進制轉換成八進制




#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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章