一组数据中只有一个数字出现了一次,其他所有数字都是成对出现的,请找出这个数字。
(使用位运算)
直接使用异或运算。
代码如下:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int arr[]={3,5,9,2,5,3,2};
int size=sizeof(arr)/sizeof(arr[0]);
int i=0,find=0;
for(;i<size;i++)
{
find^=arr[i];//循环进行异或运算
}
printf("%d\n",find);
system("pause");
return 0;
}
转载于:https://blog.51cto.com/luoyafei/1708445