【华为机试】进制转换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/soeben/article/details/79619708

题目描述:

写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 )


输入描述:

输入一个十六进制的数值字符串。


输出描述:

输出该数值的十进制字符串。

示例1
输入
0xA
输出
10

参考程序1:

#include <iostream>
using namespace std;
int main(){
    int a;
    while(cin>>hex>>a)
        cout<<a<<endl;
    return 0;
}

参考程序2:

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main(){
    string s;
    int sum=0,count=0;
    while(cin>>s){
        count = s.size()-1;
        sum = 0;
        for(int i = count;i>=2;--i)
            sum+=(s[i]>'9'?s[i]-'A'+10:s[i]-'0')*pow(16,count-i);
        cout<<sum<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/soeben/article/details/79619708