题解 | 求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) }