Description
给定些数字,这些数中只有一个数出现了奇数次,找出这个数。
Input
每组数据第一行n表示数字个数,1 <= n <= 2 ^ 18 且 n % 2 == 1。
接下来n行每行一个32位有符号整数。
Output
出现奇数次那个数,每组数据对应一行。
Sample Input
5 1 1 2 2 3 7 1 2 1 2 2 3 3
Sample Output
3 2
这题本来的思路是开数组,然后一个个判断个数,就炸了。
然后由异或的知识,我们可以直接消去偶数个相同的数字,贼带感。
#include <iostream>
#include <cstdio>
#include <string>5
#include <string.h>
#include <queue>
using namespace std;
int n,ans;
int main()
{
while(scanf("%d",&n)!=EOF)
{
int x;
ans=0;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
ans^=x;
}
printf("%d\n",ans);
}
return 0;
}
/**********************************************************************
Problem: 1217
User: jk1601zr
Language: C++
Result: AC
Time:408 ms
Memory:2024 kb
**********************************************************************/