题解 | #进制转换#
进制转换
http://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
import java.util.HashMap; import java.util.Map; import java.util.Scanner;
public class Main { public static void main(String[] args) { Map<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); map.put('a', 10); map.put('b', 11); map.put('c', 12); map.put('d', 13); map.put('e', 14); map.put('f', 15); Scanner sc = new Scanner(System.in); String str = sc.nextLine(); str = str.substring(2); int len = str.length() - 1; int num = 0; for(int i = 0;i < str.length();i++) { for (char key : map.keySet()) { if(key == str.charAt(i)) { num += map.get(key)*fang(len-i); } } } System.out.println(num); sc.close(); } private static int fang(int num) { if(num == 0) return 1; else return 16 * fang(num - 1); } }