Problem Description
In the new year party, everybody will get a "special present".Now it's your turn to get your special present, a lot of presents now putting on the desk, and only one of them will be yours.Each present has a card number on it, and your present's card number will be the one that different from all the others, and you can assume that only one number appear odd times.For example, there are 5 present, and their card numbers are 1, 2, 3, 2, 1.so your present will be the one with the card number of 3, because 3 is the number that different from all the others.
 
Input
The input file will consist of several cases.  Each case will be presented by an integer n (1<=n<1000000, and n is odd) at first. Following that, n positive integers will be given in a line, all integers will smaller than 2^31. These numbers indicate the card numbers of the presents.n = 0 ends the input.
 
Output
            For each case, output an integer in a line, which is the card number of your present.
 
Sample Input
5
1 1 3 2 2
3
1 2 1
0
 
Sample Output
3
2
Hint

Hint

use scanf to avoid Time Limit Exceeded
 
Author
8600
 
Source
HDU 2007-Spring Programming Contest - Warm Up (1)
 
Recommend
8600
 

题目内存限制:1024K,所以不能简单地用数组存然后再处理。

这题有一个好的方法—位异或。

位异或的运算法则吧:

1、a^b = b^a。

2、(a^b)^c = a^(b^c)。

3、a^b^a = b。

对于一个任意一个数n,它有几个特殊的性质:

1、0^n = n。

2、n^n = 0。

所以可以通过每次异或运算,最后剩下的值就是出现奇数次的那个数字。

#include <stdio.h> 
int main() 

    int n,x,ans; 
    while(scanf("%d",&n),n) 
    { 
        ans = 0; 
        while(n--) 
        { 
            scanf("%d",&x); 
            ans ^= x; 
        } 
        printf("%d\n",ans); 
    } 
    return 0; 
}

Find your present (2) (位异或)的更多相关文章

  1. HDU 2095 find your present (2)

    HDU 2095 find your present (2) 解法一:使用set 利用set,由于只有一个为奇数次,对一个数进行查询,不在集合中则插入,在集合中则删除,最后剩下的就是结果 /* HDU ...

  2. HDOJ(HDU) 1563 Find your present!(异或)

    Problem Description In the new year party, everybody will get a "special present".Now it's ...

  3. find your present (2) 2095

    Problem Description In the new year party, everybody will get a "special present".Now it's ...

  4. 跳转时候提示Attempt to present on while a presentation is in progress

    出现这种情况,例如:我在获取相册图片后,直接present到另一个页面,但是上一个页面可能还未dismiss,所以,要在获取相册图片的dismiss方法的complete的block里面写获取图片及跳 ...

  5. find your present (感叹一下位运算的神奇)

    find your present (2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. HTTP Status 400 - Required String parameter 'userName' is not present 错误

    HTTP Status 400 - Required String parameter 'userName' is not present 错误 先mark  有时间详细写 参考链接: https:/ ...

  7. Linux 克隆虚拟机引起的“Device eth0 does not seem to be present, delaying initialization”

    虚拟机Vmware上克隆了一个Red Hat Enterprise Linx启动时发现找不到网卡,如下所示,如果你在命令窗口启动网络服务就会遇到"Device eth0 does not s ...

  8. required string parameter XXX is not present

    @RequestParam jQuery调用方式: deleteFile: function(filePath) { return ajax({ method: 'POST', url: '/cm/s ...

  9. 启动网卡报:Device eth0 does not seem to be present”解决办法

    Device eth0 does not seem to be present”解决办法 : 用ifconfig查看发现缺少eth0,只有lo:用ifconfig -a查看发现多出了eth1的信息. ...

随机推荐

  1. 20169210《Linux内核原理与分析》第九周作业

    第一部分:实验 首先还是网易云课堂的学习,这次的课程是可执行程序的装载. 预处理.编译和链接: 可执行程序是怎么来的 以c语言代码为例的话,经过预处理,编译成汇编代码,再汇编成目标码再链接可执行文件. ...

  2. [Node.js] Exporting Modules in Node

    In this lesson, you will learn the difference between the exports statement and module.exports. Two ...

  3. OC学习笔记[注意事项]

    alloc  new  retain之后都必须要调用release方法 计数器要变只有这几种方法 retain release alloc new copy方法才会使计数器改变,谁想用人家对象,就对他 ...

  4. j2ee学习笔记 Filter过滤器

    作用:过滤response和request对象的内容 使用: Filter是J2EE的一个接口,自定义Filter需要实现该接口,并重写所有的方法. Filter提供的方法: init() doFil ...

  5. iOS 9 关键字的简单使用

    前言: 在iOS 9 苹果推出了很多关键字, 目的其实很明确, 主要就是提高开发人员的效率, 有益于程序员之间的沟通与交流, 在开发中代码更加规范! 1. nullable 与 nonnull nul ...

  6. iis6配置使用页面Gzip压缩提速

    iis7默认就启用了Gzip压缩,节约带宽,流量,能够很明显的提升访问速度,但是iis6则没有,本文就是介绍如何通过配置开启iis6的Gzip压缩 一. HTTP压缩概述 HTTP压缩是在Web服务器 ...

  7. angularjs 根据变量改变 动态加载模板

    directive return { restrict: 'E', replace: true, templateUrl: 'app/view/order.html', link: function ...

  8. awk实用技巧

    awk '{sum+=$1} END {print "Sum = ", sum}' awk '{sum+=$1} END {print "Average = " ...

  9. idea集成svn插件

    1.需要在机器上安装一个SVN客户端命令行程序,可以到这里下载对应的安装程序:http://subversion.apache.org/packages.html#windows 我选择的是torto ...

  10. android获取Mac地址和IP地址

    获取Mac地址实际项目中测试了如下几种方法:(1)设备开通Wifi连接,获取到网卡的MAC地址(但是不开通wifi,这种方法获取不到Mac地址,这种方法也是网络上使用的最多的方法) //根据Wifi信 ...