本文共 371 字,大约阅读时间需要 1 分钟。
题目:一个整型数组里除了两个数字之外,其它的数字都出现了两次。找出这两个数字。
例如数组:{2,4,3,6,3,2,5,5} 返回6和4
代码实现
#includeint s[8]={ 2,4,3,6,3,2,5,5};int main(){ int temp = 0; for(int x=0;x<8;x++) temp^=s[x]; temp = temp & (-temp); //第一个为1的位置 int s1 = 0, s2 = 0; for(int x=0;x<8;x++){ //分成两个数组 if(s[x]&temp) s1^= s[x]; else s2^=s[x]; } printf("%d %d",s1,s2);}
结果