原题链接:传送门
这道题可以用set写,但是发现用二进制的按位异或会更加简单。
按位异或是相同位为0,不同位为1.
如:
1^1 = 0
1^0 = 1
1^2^1 = 1^1^2 = 2
所以可以通过异或来找这个只出现了一次的值。
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int n,x;
while(~scanf("%d",&n)) {
int ans = 0;
for(int i=0; i<n; i++) {
scanf("%d",&x);
ans ^= x;
}
printf("%d\n",ans);
}
return 0;
}