華爲機試-字符轉換

#include<iostream>
#include<string>
#include<math.h>
using namespace std;

int main()
{
    string szInput;
    int nSum = 0;
    while(cin>>szInput)
    {
        int nLength = szInput.length();
        for(int index = nLength - 1;index >= 2;index--)
        {
            if(szInput[index] >= '0' && szInput[index] <= '9')
            {
                nSum = nSum + pow(16, (nLength - 1 - index))*(szInput[index] - 48);
            }
            else if (szInput[index] >= 'A' && szInput[index] <= 'F')
            {
                nSum = nSum + pow(16, (nLength - 1 - index))*(szInput[index] - 55);
            }
        }
        cout<<nSum<<endl;
    }
    return 0;
}
發佈了58 篇原創文章 · 獲贊 46 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章