题解 | #进制转换#
进制转换
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) { HashMap<Character, Integer> numMapper = new HashMap<Character, Integer>() { { put('0', 0); put('1', 1); put('2', 2); put('3', 3); put('4', 4); put('5', 5); put('6', 6); put('7', 7); put('8', 8); put('9', 9); put('A', 10); put('B', 11); put('C', 12); put('D', 13); put('E', 14); put('F', 15); } }; Scanner in = new Scanner(System.in); String ox = in.nextLine().substring(2).toUpperCase(); // 注意 hasNext 和 hasNextLine 的区别 int scale = 1; int sum = 0; for (int i = ox.length() - 1; i >= 0; i--) { sum += numMapper.get(ox.charAt(i)) * scale; scale *= 16; } System.out.println(sum); } }