题解 | #查找输入整数二进制中1的个数#
查找输入整数二进制中1的个数
https://www.nowcoder.com/practice/1b46eb4cf3fa49b9965ac3c2c1caf5ad
import java.util.*; public class Main { public static void main(String[] args) { Scanner fzhinput = new Scanner(System.in); String erjz; Long num; int sum; while (fzhinput.hasNextLine()){ num = fzhinput.nextLong(); fzhinput.nextLine(); erjz = Long.toString(num, 2); sum = 0; for (int i = 0; i < erjz.length(); i++) { if (erjz.charAt(i) == '1') { sum++; } } System.out.println(sum); } } }