题解 | 进制转换

进制转换

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;
const map = new Map([
    ['1', 1], ['2', 2], ['3', 3], ['4', 4], ['5', 5], ['6', 6], ['7', 7],
    ['8', 8], ['9', 9], ['0', 0], ['A', 10], ['B', 11], ['C', 12], ['D', 13],
    ['E', 14], ['F', 15]
])
void async function () {
    // Write your code here
    while(line = await readline()){
        // console.log(parseInt(line, 16))
         line = line.slice(2);
         const n = line.length;
         let x = 1;
         let res = 0;
         for (let i = n - 1; i >= 0; i -= 1) {
             res += map.get(line[i])* x;
             x *= 16;
         }
         console.log(res);
    }
}()

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务