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. SAP财务凭证冲销

    爱思普信息咨询/SAP Partner网(SAP软件/ERP介绍/SAP All-in one介绍) 客户公司的同事有时经常会问到冲销的问题,可能大部份的用户对于财务的冲销功能都不是很理解,这里我们全 ...

  2. MongoDB 复制集 (一) 成员介绍

       一 MongoDB 复制集简介          MongoDB的复制机制主要分为两种:          Master-Slave    (主从复制)      这个已经不建议使用       ...

  3. 推荐一个可以直接在Visual Studio中看到complexity的插件CodeMaid

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:推荐一个可以直接在Visual Studio中看到complexity的插件CodeMaid.

  4. JMeter入门(2):一个简单实例

    场景描述: 自己建立一个服务器端,接受参数:name和age,并将这些数据保存到数据库中: http://localhost:8080/Server/SaveServlet?name=xxx& ...

  5. java web应用下跨域3招

    一.设置服务器端,让ajax能直接调用 服务器端设置 tomcat 设置为例: 在web.xml中添加如下过滤器 <filter> <filter-name>CorsFilte ...

  6. Java 反射学习笔记

    要学反射,先要了解Class这个类,Class是所有Java类的一个总称,Class的实例中存储的是一个类的字节码,获取Class的实例有三种方式: System.class new Date().g ...

  7. Java基础知识强化之IO流笔记32:转换流之OutputStreamWriter的使用

    1. OutputStreamWriter的使用 OutputStreamWriter(OutputStream out):根据默认编码把字节流的数据转换为字符流 OutputStreamWriter ...

  8. runtime重写description方法打印model属性和值

    在开发过程中, 往往会有很多的model来装载属性. 而在开发期间经常会进行调试查看model里的属性值是否正确. 那么问题来了, 在objective-c里使用NSLog("%@" ...

  9. centos 安装mysqldb 记录

    vim setup_pofix.py #修改mysql_config路径 <pre> ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /us ...

  10. Gradle插件

    1.方法数统计 classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.6.1' apply plugin: 'com.getkeep ...