<-求赞
思路
直接x -= lowbit(x),减多少次就说明有多少个1
#include<iostream>
using namespace std;
int lowbit(int x)
{
return x&-x;
}
int main()
{
int n;
cin>>n;
while(n--)
{
int x;
cin>>x;
int res=0;
while(x) x-=lowbit(x),res++;
cout<<res<<endl;
}
return 0;
}
继续搞逝#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (n -- ){ int x; cin >> x; bitset <32> b = x; cout << b.count() << ' '; } return 0; }
##
STL打法好(doge)