HDU 2095 find your present (2)
HDU 2095 find your present (2)
解法一:使用set
利用set,由于只有一个为奇数次,对一个数进行查询,不在集合中则插入,在集合中则删除,最后剩下的就是结果
/* HDU 2095 find your present (2) --- 水题 */
#include <cstdio>
#include <set>
#include <algorithm>
using namespace std; int main()
{
#ifdef _LOCAL
freopen("D:\\input.txt","r", stdin);
#endif
int n,tmp; set<int> s;
while (scanf("%d", &n) == && n != ){
s.clear();
for (int i = ; i < n; ++i){
scanf("%d", &tmp);
if (s.find(tmp) == s.end())
s.insert(tmp);
else
s.erase(tmp);
}
printf("%d\n", *s.begin());
} return ;
}
解法二:位异或
有离散数学可知,异或运算具有以下性质:
1.a^b = b^a; //交换律
2.(a^b)^c = a^(b^c); //结合律
3.a^b^a = b; a^b^b = a;
4.0^n = n;
5.n^n=0;
因此将这n个数对0进行n次异或得到的结果即为想要的结果。
/* HDU 2095 find your present (2) --- 位异或 */
#include <cstdio> int main()
{
#ifdef _LOCAL
freopen("D:\\input.txt", "r", stdin);
#endif
int n, tmp;
while (scanf("%d", &n) == && n){
int ans = ;
for (int i = ; i < n; ++i){
scanf("%d", &tmp);
ans ^= tmp;
}
printf("%d\n", ans);
} return ;
}
HDU 2095 find your present (2)的更多相关文章
- hdu 2095 find your present (2) 位运算
题意:找出现次数为奇数的数 xor运算 #include <cstdio> #include <iostream> #include <algorithm> usi ...
- HDU 2095 find your present (2) (异或)
题意:给定n个数,让你找出那一个次数为1的. 析:由于题意说了,只有那一个数是奇数,所以其他的都是偶数,根据异或的性质,两个相同的数异或为0: 任何数和0异或得原数,可以很简单的做出这个题. 代码如下 ...
- HDU 2095 find your present (2) 动态链表
解题报告:输入一个n,后面紧跟着输入n个数,输入的这n个数中,除了有一个数的个数为奇数外,其它的数的个数都是偶数个,现在要你找出这个个数为奇数的这个数. 看起来好像很简单的样子,不过,这题的重点不在这 ...
- HDU 2095 find your present (2)( 位运算 )
链接:传送门 题意:给出n个数,这n个数中只有一种数出现奇数次,其他全部出现偶数次,让你找到奇数次这个数 思路:简单异或运算题 /*********************************** ...
- HDU.2095(异或运算)
find your present (2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 杭电 2095 find your present (2)【位运算 异或】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2095 解题思路:因为只有我们要求的那个数出现的次数为奇数,所以可以用位运算来做,两次异或同一个数最后结 ...
- hdu 1563 Find your present!
Find your present! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDOJ(HDU) 1563 Find your present!(异或)
Problem Description In the new year party, everybody will get a "special present".Now it's ...
- hdu 2095
ps:真是日了狗...英语渣渣理解题目不行,开了个100W数组来算,还优化了下时间,还是超时了,看了题解才知道用异或. N个数异或,会得出其中是奇数的一个.比如 1^1^3^2^2 = 3. 1^ ...
随机推荐
- cvWaitKey
OpenCV中的一个函数 函数原型为: C++: int waitKey(int delay=0) Python: cv2.waitKey([delay]) → retval C: int cvWai ...
- SharePoint 2013 Nintex Workflow 工作流帮助(二)
博客地址 http://blog.csdn.net/foxdave 工作流动作 1. Action Set(Logic and flow分组) 它是一个工作流的集合,可以理解为容器的东西.所以它本身并 ...
- IOS NSInvocation用法简介
IOS NSInvocation用法简介 2012-10-25 19:59 来源:博客园 作者:csj007523 字号:T|T [摘要]在 iOS中可以直接调用某个对象的消息方式有两种,其中一种就是 ...
- hdu 2071
Ps:输出n个数里最大的 #include "stdio.h" int main(){ ],max; int i,j,n,t; while(~scanf("%d" ...
- 数据结构 《6》----堆 ( Heap )
Practival Problems: a. Construct a Huffman code b. Compute the sum of a large set of floating point ...
- 初始React Native
1.何是React Native: React-Native是:Facebook 在2015年初React.js技术研讨大会上公布的一个开源项目.支持用开源的JavaScript库React.js来开 ...
- css position: absolute、relative详解
CSS2.0 HandBook上的解释: 设置此属性值为 absolute 会将对象拖离出正常的文档流绝对定位而不考虑它周围内容的布局.假如其他具有不同 z-index 属性的对象已经占据了给定的位置 ...
- [转]Why Not Paxos
http://blog.csdn.net/cszhouwei/article/details/38374603 Why Not Paxos Paxos算法是莱斯利·兰伯特(LeslieLamport, ...
- matio使用
http://na-wiki.csc.kth.se/mediawiki/index.php/MatIO (1)build根据教程 (2)sudo ldconfig (3)写main根据链接:修改几个类 ...
- Qt4升级到Qt5
QtWidgets作为一个独立的模块 例如编译时错误 error: QMainWindow: No such file or directory error: QToolButton: No such ...