题解 | #进制转换#

进制转换

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
    const hexToDec = {
        a: 10,
        b: 11,
        c: 12,
        d: 13,
        e: 14,
        f: 15,
        g: 16
    }
    const isDigit = (char) =>{
        return /^\d+$/.test(char);
    }
    const isAlpha = (char) =>{
        /^[a-zA-Z]+$/.test(char);
    }
    line = await readline();
    // let len = line.length - 2;
    let res = 0;
    for(let i = line.length-1; i > 1; i--){
        // let targetNum = isDigit(line[i])?parseInt(line[i]):hexToDec[line[i].toLowerCase()];
        let targetNum = line[i]<10 ? parseInt(line[i]):hexToDec[line[i].toLowerCase()];
        // console.log(line[i]);
        // console.log(targetNum)
        res +=  targetNum* Math.pow(16, line.length - i - 1);
    }
    console.log(res); 
}()

全部评论

相关推荐

2024-11-21 14:22
门头沟学院 硬件开发
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务