找筷子(异或运算)

题目链接:https://www.luogu.com.cn/problem/P1469#submit

题意:找奇数个筷子的长度

思路:异或运算(如果开map会MLE)

按位异或运算:每个位对比相同为0不同为1

重要性质: x^x=0, x^0=x 所以我们可以直接算出所有数的异或和,即为答案

其他性质(待补充):异或运算顺序不重要

#include<bits/stdc++.h>
using namespace std;
int n;
int ans=0;
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int x;cin>>x;
		ans=ans^x;
	}
	cout<<ans;
	return 0;
}


posted @ 2025-01-01 20:38  Marinaco  阅读(84)  评论(0)    收藏  举报
//雪花飘落效果