码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-09-15 03:17:58      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   for   div   sp   log   on   

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

 

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

 

 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 }

 

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

标签:style   blog   color   io   for   div   sp   log   on   

原文地址:http://www.cnblogs.com/catdrivedragon/p/3972070.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!