题解 | #HJ62 查找输入整数二进制中1的个数#
查找输入整数二进制中1的个数
http://www.nowcoder.com/practice/1b46eb4cf3fa49b9965ac3c2c1caf5ad
#include <stdio.h>
int countOnes(int x) {
int ones = 0;
while (x > 0) {
x &= (x - 1);
ones++;
}
return ones;
}
int main() {
int n;
int count;
while (scanf("%d", &n) != EOF) {
count = countOnes(n);
printf("%d\n", count);
}
return 0;
}