题解 | 出现奇数次的数
出现奇数次的数
https://www.nowcoder.com/practice/b3bd694fd4c44b89a5c6b49d855121b8
异或是“相同为0,不同为1”。
所以对于任意的x都有:x ^ x = 0。
如果一个数异或自己本身,则贡献值为0。即出现偶数次的数贡献值必然为0。
所以唯一出贡献的只有出现次数为奇数的数。
#include <iostream>
using namespace std;
int main() {
int n, s, ans = 0;
cin >> n;
for(int i=0;i<2*n+1;i++){
cin >> s;
ans ^= s;
}
cout << ans;
}
OPPO公司福利 1225人发布
查看15道真题和解析