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的信息. ...
随机推荐
- DevExpress 用户控件 分页(下)
分页控件调用 (1)初始化时: this.pageCtrl1.pageSize = 4; (2)数据绑定时: 从数据库中获取实时的 Public void LoadData(){ //这是只写有关分页 ...
- js_面向对象编程
主要内容 值类型与引用类型的特征 深拷贝与浅拷贝 对象的动态特性 构造函数的执行过程 异常处理 dom操作(略) <!DOCTYPE html> <html> <head ...
- asp.net采用OLEDB方式导入Excel数据时提示:未在本地计算机上注册"Microsoft.Jet.OLEDB.4.0" 提供程序"
笔者在项目中做做了一个从Excel表格中导入数据的模块.大体上asp.net项目中导入Excel大体分成三类: 1)采用c#内置方案System.Data.OleDb(限制较小, 通用) 2)采用Ex ...
- Unity3D中Prefab
Prefab概念: Prefab是一种资源类型--存储在项目视图中的一种可反复使用的游戏对象.因而当游戏中须要非常多反复使用的对象.资源等时,Prefab就有了用武之地.它拥有下面特点: 能够放到多个 ...
- FontCombobox 和FontSizeCombobox
附件:http://files.cnblogs.com/xe2011/WindowsFormsFontCombox.rar 自定义组件字体组合框 自定义组件字体组合框如何使用 自定义组件字体大小组合框 ...
- hadoop错误ERROR namenode.NameNode (NameNode.javamain(1657)) - Failed to start namenode java.net.BindException:Port in use:host1:50070
解决方法: 1.通过lsof -i:50070(lsof可以通过yum install lsof安装)查看,发现是mysql被占用了 2.修改mysql端口 从/usr/share/mysql/my- ...
- Chapter 2 - How to Add a sprite
Chapter 2 - How to Add a sprite 1. Add image resources 1.1add resources on win32 2. Add a sprite TIP ...
- windows系统 安装MongoDB 32位
本篇文章记录了我在win7 32位下安装MongoDB的步骤,以作记录. 下文的安装方法参考了以下博文: http://www.cnblogs.com/lzrabbit/p/3682510.html ...
- 11.10 noip模拟试题
1.第K小数 (number.cpp/c/pas) [问题描述] 有两个正整数数列,元素个数分别为N和M.从两个数列中分别任取一个数 相乘,这样一共可以得到N*M个数,询问这N*M个数中第K小数是多少 ...
- 页面跳转 url地址的写法
跳转地址:分两类,wikipage和aspx页面: wikipage:当新建webpart,在网站里新建一个wikipage,然后将webpart添加进wikipage,这种情况下跳转页面需要添加si ...