题解 | 求int型正整数在内存中存储时1的个数

求int型正整数在内存中存储时1的个数

https://www.nowcoder.com/practice/440f16e490a0404786865e99c6ad91c9

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
let target:string[] = []
rl.on('line', function (line) {
    target = line.split(' ');
}).on("close",()=>{
    console.log(decimalTentoTwo(Number(target[0])))
})
function sumChar(str:any){
    let sum:number = 0
    for(const s in str){
        if(str[s]==="1"){
            sum++
        }
    }
    return sum
}
function decimalTentoTwo(hex:number){
    let decimalstr:string = ""
    while(hex>0){
        decimalstr = hex%2 + decimalstr
        hex = Math.floor(hex/2)
    }
    return sumChar(decimalstr)
}

全部评论

相关推荐

07-02 22:46
门头沟学院 Java
码农索隆:hr:“管你投没投,先挂了再说”
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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