题解 | #进制转换#
进制转换
http://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
str = str.trim().substring(2);
char[] chrs = str.toCharArray();
HashMap<Character, Integer> CharacterToInteger = new HashMap<>();
for (int i = 0; i < 10; i++) {
CharacterToInteger.put((char) ('0' + i), i);
}
for (int i = 10; i < 16; i++) {
CharacterToInteger.put((char) ('A' + i - 10), i);
}
int ans = 0;
for (int i = chrs.length - 1; i > -1; i--) { // 从数组的末尾开始往前遍历
char chr = chrs[i]; // 获取当前位置上的字符
int val = CharacterToInteger.get(chr); // 获取当前字符对应的数字
ans += val * (int) Math.pow(16, chrs.length - 1 - i);
}
System.out.println(ans);
}
}