资源限制
时间限制:1.0s 内存限制:512.0MB
问题描述
从键盘输入一个不超过8位的正的十六进制数字符串,将它转换为正的十进制数后输出。
注:十六进制数中的10~15分别用大写的英文字母A、B、C、D、E、F表示。
样例输入
FFFF
样例输出
65535
Python
str_hex=input()
result=int(str_hex,16)
print(result)
C++
注意:ffffffff =(十进制)4294967295,超出int范围,所以转换成的十进制数的类型应该是long long
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int main() {
string str_hex = "";
cin >> str_hex;
int len_hex = str_hex.length();
long long result = 0, temp = 0;
for (int i = 0; i <len_hex; i++) {
temp = pow(16, len_hex - 1 - i);
switch (str_hex[i]) {
case 'A':temp = temp * 10; break;
case 'B':temp = temp * 11; break;
case 'C':temp = temp * 12; break;
case 'D':temp = temp * 13; break;
case 'E':temp = temp * 14; break;
case 'F':temp = temp * 15; break;
default:temp = temp * (str_hex[i] - '0'); break;
}
result += temp;
}
cout << result << endl;
return 0;
}
Java
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str_hex = sc.next();
BigInteger result = new BigInteger(str_hex,16);
System.out.println(result);
}
}