题解 | #查找输入整数二进制中1的个数#
查找输入整数二进制中1的个数
https://www.nowcoder.com/practice/1b46eb4cf3fa49b9965ac3c2c1caf5ad
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int num = in.nextInt(); //Integer包装类的toBinaryString方法转二进制字符串 String binaryNum = Integer.toBinaryString(num); //去掉二进制字符串的1保留0 String ans = binaryNum.replace("1", ""); System.out.println(binaryNum.length() - ans.length()); } } }