Problem Description
告诉你一串数字,其中几乎所有数字都出现偶数次,但有一个例外,其中有一个数字出现了奇数次,请输出这个数字。
Input
一个n(1<=n<1e6,且n为奇数)和n个数字ai(1<=ai<=1e14),多组输入。
Output
输出只出现奇数次的数字。
Sample Input
3
1 3 3
9
2 5 7 2 6 5 6 7 1
Sample Output
1
1
Author
niconiconi
题解:异或运算:0^1=1,1^1=0.总之:a^b^b=a;
出现偶数次的数字与0异或后都是0,只有奇数次的数与0异或,才是它本身。
代码如下:(这道题数据不好,直接暴力也行)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
long long int a,j=0;
int k;
for(k=0;k<n;k++)
{
scanf("%lld",&a);
j^=a;
}
printf("%lld\n",j);
}
return 0;
}