寻找数组中唯一只出现1次的数

一个数组中除开唯一一个元素只出现一次,其余的元素均出现三次。

 

位运算,看不明白,留着以后看吧。

 

 1 #include <stdio.h>
 2 #include <algorithm>
 3 using namespace std;
 4 #define maxn 1000
 5 int a[maxn],n;
 6 
 7 int main()
 8 {    
 9     while(scanf("%d",&n), n)
10     {
11         int one = 0, two = 0, three = 0;
12         for(int i=0; i<n; i++)
13         {
14             scanf("%d",&a[i]);
15             three = two & a[i];
16             two = two | one & a[i];
17             one = one | a[i];
18             two = two & ~three;
19             one = one & ~three;
20         }      
21         printf("%d\n",one);
22     }
23     return 0;
24 }

 

posted @ 2014-09-15 02:34  猫御龙  阅读(316)  评论(0编辑  收藏  举报