HDOJ 2095 find your present (2)

http://acm.hdu.edu.cn/showproblem.php?pid=2095

好题,用STL MAP MLE了,Google了一下,我表示这个想法我自己是想不到的,好多大牛。。。

 

主要原理 1个数异或他自己 =0

              1个数异或0 = 他自己

理解了上面这个就好办了,题目中说的其他数字都出现偶数次,只一个数出现一次

              如果 将 0异或他们全部,结果将是只出现一次的那个数

 

还有帮助理解的一点,异或满足交换律~~~

 

 1 #include <stdio.h>
2 int main()
3 {
4 int n,value,result=0;
5 while(scanf("%d",&n)!=EOF){
6 result=0;
7 if(!n)
8 break;
9 int i;
10 for(i=0;i<n;i++){
11 scanf("%d",&value);
12 result^=value;
13 }
14 printf("%d\n",result);
15 }
16 }



posted @ 2011-12-02 10:27  linyvxiang  阅读(163)  评论(0编辑  收藏  举报