Description
给你一个整数数列,保证只有一个数出现过奇数次,输出它。
Input
多组测试数据。 每组测试数据第一行为一个整数n,代表该数列元素个数。(1 <= n <= 500000) 第二行为n个整数ai,以空格隔开。(-1000000 <= ai <= 1000000)
Output
输出一行表示这个出现奇数次的数。
Sample Input
5
2 3 2 3 1
7
6 6 6 2 6 6 6
Sample Output
1
2
核心思想:
异或运算,ans异或任一数字两次,其值不变。
代码如下:
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
int main()
{
int n,x,ans;
while(~scanf("%d",&n))
{
ans=0;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
ans^=x;
}
printf("%d\n",ans);
}
return 0;
}