题解 | 进制转换

进制转换

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 () {
    let num = await readline()
    num = num.substring(2,num.length)//去掉仅表示格式的部分

    let ans = 0
    
    num = num.split('').reverse()

    for(let i=0;i<num.length;i++){
        let temp
        switch(num[i]){
            case "A": {
                temp=10
                break
            }
            case "B": {
                temp=11
                break
            }
            case "C": {
                temp=12
                break
            }
            case "D": {
                temp=13
                break
            }
            case "E": {
                temp=14
                break
            }
            case "F": {
                temp=15
                break
            }
            default: temp = num[i]
        }
        ans += temp*Math.pow(16,i)

    }
    console.log(ans)
}()

笨蛋级别的做法。不过倒是也获取了新的知识点:`parseInt()`

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    let num = await readline()
    console.log(parseInt(num,16))
	//parseInt(string, radix) 解析一个字符串并返回指定基数的十进制整数,radix 是 2-36 之间的整数,表示被解析字符串的基数。
}()

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务