题解 | #进制转换#
进制转换
https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
方法1:
while(line = readline()){ let num = line; console.log(parseInt(num, 16)); }
因为JavaScript语言本身就有数值转换的方法,此题可以直接用parseInt(string,radix)来指定基数,将第一个参数指定基数并转换为十进制整数。
方法2:
let template = { "A" : 10, "a" : 10, "B" : 11, "b" : 11, "C" : 12, "c" : 12, "D" : 13, "d" : 13, "E" : 14, "e" : 14, "F" : 15, "f" : 15 }; while(line = readline()){ let str = line.slice(2); let rst = 0; for(let i = str.length - 1; i >= 0; i --){ rst += (template[str[i]] || str[i]) * Math.pow(16, str.length - 1 - i); } console.log(rst); }