寻找数组中唯一只出现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 }
必须不断的提醒自己:放下浮躁,静心阅读;放下担忧,勇敢走自己的路;放下贪婪,有失必有得;放下自卑,相信自己;放下虚荣,别自以为是;放下自私,学会懂得感恩;放下懒惰,继续努力。