题解 | 进制转换
进制转换
https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String a = in.nextLine();
Map<String, Integer> hexMap = new HashMap<>();
hexMap.put("A", 10);
hexMap.put("B", 11);
hexMap.put("C", 12);
hexMap.put("D", 13);
hexMap.put("E", 14);
hexMap.put("F", 15);
int result = 0;
a = a.substring(2, a.length());
for (int i = a.length(); i >= 1; i--) {
int first = 0;
String str = a.substring(i-1,i);
int descIndex = a.length() - i;
if (hexMap.containsKey(str)) {
first = hexMap.get(str);
} else {
first = Integer.parseInt(str);
}
result += first * Math.pow(16, descIndex) ;
}
System.out.println(result);
}
}
}
