原题链接:https://www.acwing.com/problem/content/803/

思路:

  • 采用lowbit()函数
  • 采用位运算
#include <iostream>


int bitCount(unsigned int n) {
n = ((n & 0xAAAAAAAA) >> 1) + (n & ~0xAAAAAAAA);
n = ((n & 0xCCCCCCCC) >> 2) + (n & ~0xCCCCCCCC);
n = ((n & 0xF0F0F0F0) >> 4) + (n & ~0xF0F0F0F0);
n = ((n & 0xFF00FF00) >> 8) + (n & ~0xFF00FF00);
n = ((n & 0xFFFF0000) >> 16) + (n & ~0xFFFF0000);
return n;
}


int count(int n) {
int res = 0;
while (n != 0) {
n -= (n & -n);
res++;
}
return res;
}

int main() {
int n;
scanf("%d", &n);
int num[n];
for (int i = 0; i < n; i++) scanf("%d", &num[i]);

for (int i = 0; i < n; i++) printf("%d ", bitCount(num[i]));
return 0;
}