算法
位运算
思路
1.用短除法求出1的个数,同时进行统计
2.完成1.
3.完成2.
C++ 代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1e5+1e4;
ll n,sr,ans;
int main()
{
ios::sync_with_stdio(false);
cin>>n;
for(ll i=1;i<=n;i++)
{
ans=0;
cin>>sr;
while(sr!=0)
{
if(sr%2==1)ans++;//mod2为1说明2进制下该位为1
sr/=2;//短除法,/2
}
cout<<ans<<" ";
}
return 0;
}