《二进制中1的个数》

51 阅读1分钟
#include <bits/stdc++.h>

using namespace std;

const int N = 100010;

int a[N];

int lowbit(int k)
{
	return k & -k;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	int n;
	cin >> n;

	for (int i = 0; i < n; i ++) cin >> a[i];

	for (int i = 0; i < n; i ++)
	{
		int res = 0;
		while (a[i]) a[i] -= lowbit(a[i]), res ++;
		cout << res << ' ';
	}

	return 0;
}