题解 | #查找输入整数二进制中1的个数#
查找输入整数二进制中1的个数
https://www.nowcoder.com/practice/1b46eb4cf3fa49b9965ac3c2c1caf5ad
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
//i&(i-1)可以去除最右边的1个1
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int n = sc.nextInt();
int j = n, count = 0;
while (j != 0) {
j = j & (j - 1);
count++;
}
System.out.println(count);
}
}
}

阿里云成长空间 743人发布
查看15道真题和解析