题解 | #进制转换#
进制转换
https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void (async function () { // Write your code here let map = new Map(); for (let i = 0; i < 10; i++) { map.set(String(i), i); } map.set('A', 10).set('B', 11).set('C', 12).set('D', 13).set('E', 14).set('F', 15) while ((line = await readline())) { // 方法1: // console.log(eval(line)) // 方法2: // console.log(Number(line,16)) // 方法3: // console.log(parseInt(line, 16)) // 方法4: // let str = line.substring(2, line.length) // let arr = str.toUpperCase().split('') // let result = 0 // for(let i = 0; i < arr.length; i++){ // let tempNum = Math.pow(16, arr.length - i - 1) // result += map.get(arr[i]) * tempNum // } // console.log(result) // 方法5: const len = line.length; let result = 0; for(let i = len - 1; i > 1; i--){ result += map.get(line[i]) * 16 ** (len - 1 - i); } console.log(result) } })();