题目:801. 二进制中1的个数 - AcWing题库
代码实现:
一知半解的规则记住就行了!!
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while (n-- > 0) {
int x = sc.nextInt();
int count = 0;
while (x != 0) {
x -= lowbit(x);
count++;
}
System.out.print(count + " ");
}
}
public static int lowbit(int x) {
return x & -x;
}
}