题解 | #进制转换#
进制转换
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); String hexString = in.nextLine(); hexString = hexString.substring(2); Map<Character, Integer> map = initMap(); int result = 0; for (int i = hexString.length() - 1; i >= 0; i--) { Character c = hexString.charAt(i); if (map.containsKey(c)) { result += getValue(map.get(c), hexString.length() - i); } else { result += getValue(Integer.parseInt(c.toString()), hexString.length() - i); } } System.out.println(result); } public static Map<Character, Integer> initMap() { Map<Character, 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); return hexMap; } public static int getValue (int res, int loopCount) { int value = 1; for (int i = 1; i < loopCount; i++) { value = value * 16; } return res * value; } }
主要还是通过Map来存储字符对应的值,同时需要去掉字符前边的两位。其实也可以判断下是否以0x开头,再去掉的。
最后,Interger.parse 也提供了相关的API