CodeForces 1300C Anu Has a Function

CodeForces 1300C Anu Has a Function_第1张图片CodeForces 1300C Anu Has a Function_第2张图片
解题思路
只有在某一位上有唯一的1才会对答案做出贡献,我们把所有的数字转化成二进制,然后遍历每一位上的1,剩下的直接随意输出就ok

#include 
#include 
#include 

using namespace std;

const int MAXN = 1e5 + 5;

int a[MAXN], vis[MAXN];

int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		cin >> a[i];
	}
	int tmp = 0;
	for (int i = 32; i > 0; --i)
	{
		int flag = 0;
		for (int j = 0; j < n; ++j)
		{
			if (((a[j] >> (i - 1)) & 1) == 1 && vis[j] == 0)
			{
				tmp = j;
				if (flag == 1)
				{
					flag = 0;
					break;
				}
				flag = 1;
			}
		}
		if (flag == 1)
		{
			cout << a[tmp] << " ";
			vis[tmp] = 1;
		}
	}
	for (int i = 0; i < n; ++i)
	{
		if (vis[i] == 0)
		{
			cout << a[i] << " ";
		}
	}
	return 0;
}

你可能感兴趣的:(cf)