把所有数按顺序直接异或一遍即可,因为同一个数出现偶数次会在异或中抵消为 0,最后剩下的就是出现奇数次的数字异或和。
void solve(){
int n=0,c=getchar();
while(c<'0'||c>'9')c=getchar();
while(c>='0'&&c<='9'){
n=n*10+c-'0';
c=getchar();
}
int ans=0;
for(int i=0;i<n;++i){
int x=0;
c=getchar();
while(c<'0'||c>'9')c=getchar();
while(c>='0'&&c<='9'){
x=x*10+c-'0';
c=getchar();
}
ans^=x;
}
cout<<ans<<endl;
}

京公网安备 11010502036488号