Find your present (2) (位异或)
|
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 |
|
Sample Output
3 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) (位异或)的更多相关文章
- HDU 2095 find your present (2)
HDU 2095 find your present (2) 解法一:使用set 利用set,由于只有一个为奇数次,对一个数进行查询,不在集合中则插入,在集合中则删除,最后剩下的就是结果 /* HDU ...
- HDOJ(HDU) 1563 Find your present!(异或)
Problem Description In the new year party, everybody will get a "special present".Now it's ...
- find your present (2) 2095
Problem Description In the new year party, everybody will get a "special present".Now it's ...
- 跳转时候提示Attempt to present on while a presentation is in progress
出现这种情况,例如:我在获取相册图片后,直接present到另一个页面,但是上一个页面可能还未dismiss,所以,要在获取相册图片的dismiss方法的complete的block里面写获取图片及跳 ...
- find your present (感叹一下位运算的神奇)
find your present (2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HTTP Status 400 - Required String parameter 'userName' is not present 错误
HTTP Status 400 - Required String parameter 'userName' is not present 错误 先mark 有时间详细写 参考链接: https:/ ...
- Linux 克隆虚拟机引起的“Device eth0 does not seem to be present, delaying initialization”
虚拟机Vmware上克隆了一个Red Hat Enterprise Linx启动时发现找不到网卡,如下所示,如果你在命令窗口启动网络服务就会遇到"Device eth0 does not s ...
- required string parameter XXX is not present
@RequestParam jQuery调用方式: deleteFile: function(filePath) { return ajax({ method: 'POST', url: '/cm/s ...
- 启动网卡报:Device eth0 does not seem to be present”解决办法
Device eth0 does not seem to be present”解决办法 : 用ifconfig查看发现缺少eth0,只有lo:用ifconfig -a查看发现多出了eth1的信息. ...
随机推荐
- Jsp学习(1)
Servlet的用作:用java语言开发动态资源的技术: Jsp的作用:用java语言(+html)开发动态的资源,其实jsp就是servlet演化而来的. Jsp的执行过程: 我们先来做一个实验,首 ...
- Android项目svn代码管理问题[转]
用svn控制版本,svn本身是不会识别哪些该传,哪些不该传,这就导致有些关于路径的东西(比如拓展jar的路径)也被上传了,而当别人下载后,那个路径对于这个人可能完全不存在,项目编译就会出问题.用ecl ...
- 清理300多台MySQL数据库的过期binlog日志
早晨睡梦中,被 on-call了,说磁盘报警,赶紧起来打开email,收到上百封email报警,数据库磁盘不够了,查询了原因 [xxx@xxxx cacti]$ ssh xxxx "df - ...
- C#绘制圆形时钟
本文由作者参考部分案例后加以修改完成: 参考链接如下: http://blog.csdn.net/xuemoyao/article/details/8001113 http://wenku.baidu ...
- C++数据结构和算法每天一练(线性表)
#include <iostream> using namespace std; class ArrayLinerTable { public: void InitLine ...
- oracle修改登录认证方式
通过配置sqlnet.ora文件,我们可以修改oracle登录认证方式. SQLNET.AUTHENTICATION_SERVICES=(NTS);基于操作系统的认证 SQLNET.AUTHENTIC ...
- iTunes无法验证服务器“s.mzstatic.com.”的身份
修改hosts文件,在里面添上一行 23.214.233.166 s.mzstatic.com
- 调试php的soapServer
用.NET的webservice做调试很轻松. 用soapserver的try和cacth获取不了多少信息
- asp.net中WebForm.aspx与类文件分离使用
第一步:新建一个web项目和类库,新建一个页面和映射类文件: 第二步:在页面中,删除默认映射类,添加服务器控件. 1.更改映射类命名空间: 原: <%@ Page Language=" ...
- error: Unable to find vcvarsall.bat while install python library by pip install or python setup.py install.
Python 2.7 会搜索 Visual Studio 2008. 如果你电脑上没有这个版本的话,比如只有: 1.Visual Studio 2010,在cmd里面执行:SET VS90COMNTO ...