题解 | 进制转换
进制转换
https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
import java.util.HashMap;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String hex = in.nextLine();
int hexLength = hex.length();
int num = 0;
HashMap<Character,Integer> map = new HashMap();
map.put('0',0);
map.put('1',1);
map.put('2',2);
map.put('3',3);
map.put('4',4);
map.put('5',5);
map.put('6',6);
map.put('7',7);
map.put('8',8);
map.put('9',9);
map.put('A',10);
map.put('B',11);
map.put('C',12);
map.put('D',13);
map.put('E',14);
map.put('F',15);
for(int i=hexLength-1,j=0;i>=2;i--,j++){
num+= map.get(hex.charAt(i)) * Math.pow(16,j);
}
System.out.print(num);
}
}
}