题解 | #进制转换#
进制转换
https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
import java.util.*; import java.lang.Math; public class Main{ public static void main(String[] args){ Map<Character,Integer> charMap = new HashMap<>(); charMap.put('0',0); charMap.put('1',1); charMap.put('2',2); charMap.put('3',3); charMap.put('4',4); charMap.put('5',5); charMap.put('6',6); charMap.put('7',7); charMap.put('8',8); charMap.put('9',9); charMap.put('A',10); charMap.put('B',11); charMap.put('C',12); charMap.put('D',13); charMap.put('E',14); charMap.put('F',15); Scanner sc = new Scanner(System.in); while(sc.hasNextLine()){ String inputStr = sc.nextLine().substring(2); char[] chars = inputStr.toCharArray(); int result = 0; if(chars.length == 0){ System.out.println(0); break; } if(chars.length <= 1){ System.out.println(charMap.get(chars[0])); break; } for(int i=0;i < chars.length ;i++){ result += charMap.get(chars[i]) * Math.pow(16,chars.length - i - 1); } System.out.println(result); } } }应该属于暴力了吧?暴力加适当剪枝?