题解 | #进制转换#
进制转换
https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String input = in.nextLine(); Map<String,Integer> map = new HashMap<String,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); } }; //1,2,3,4,5,6,7,8,9,A,B,C,D,E,F //去掉前缀 input = input.substring(2,input.length()).replaceAll("\\s+","").toUpperCase(); List<String> strList = Arrays.asList(input.split("")); Collections.reverse(strList); int sum = 0; int power = 1; for(int i=0;i<strList.size();i++){ sum = sum + map.get(strList.get(i)) * power; power *= 16; } System.out.println(sum); } }