NYOJ 528 找球号(三) (异或)

题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=528

        这道题刚开始用set去写,好像是因为数据的问题吧,会mle,然后用了异或的方法就过了,一个数异或了两次以后就还是他本身,所以最后剩下的数就是只有异或了一次的数。


用set写的代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
int n;
set<int> s;

int main()
{
	while(~scanf("%d",&n)){
		for(int i=0;i<n;i++){
			int x;
			scanf("%d",&x);
			if(s.find(x) == s.end())s.insert(x);
			else s.erase(x);
		}
		int ans = *s.begin();
		printf("%d\n",ans);
		s.clear();
	}
	return 0;
}

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n,m;

int main()
{
	while(~scanf("%d",&n)){
		int ans = 0;
		for(int i=0;i<n;i++){
			scanf("%d",&m);
			ans ^= m;
		}
		printf("%d\n",ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/charles_zaqdt/article/details/81154225