#include <iostream>
using namespace std;
int n;
int lowbit(int x){// binary返回x最后一个1的位置,如1010→10
return x&(-x);
}
int main(){
scanf("%d",&n);
while(n--){
int res = 0;
int x;
scanf("%d",&x);
while(x) x-=lowbit(x),res++;//每次清除最末尾的1,清除多少次就有多少1
printf("%d ",res);
}
return 0;
}
acwing 801.二进制中1的个数
最新推荐文章于 2024-06-27 17:45:24 发布
![](https://img-home.csdnimg.cn/images/20240611030827.png)