给定多个十进制的数,求二进制情况下1的个数
#include <iostream>
using namespace std;
int lowbits(int x)
{
return x&-x;
}
int main()
{
int n; cin >> n;
while(n--)
{
int x, res = 0;
cin >> x;
while(x) x -= lowbits(x),res++;
cout << res << " ";
}
}
给定多个十进制的数,求二进制情况下1的个数
#include <iostream>
using namespace std;
int lowbits(int x)
{
return x&-x;
}
int main()
{
int n; cin >> n;
while(n--)
{
int x, res = 0;
cin >> x;
while(x) x -= lowbits(x),res++;
cout << res << " ";
}
}