find your present (2) 2095
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.
1 1 3 2 2
3
1 2 1
0
2
Hint
use scanf to avoid Time Limit Exceeded
//map
#include <set>
#include <stdio.h>
using namespace std;
int main()
{
int n,x;
set <int> S;
while(scanf("%d",&n),n)
{
while(n--)
{
scanf("%d",&x);
if(S.find(x) == S.end()) //没找到,插入
S.insert(x);
else //找到了,删除
S.erase(x);
}
printf("%d\n",*S.begin());
S.clear();
}
return ;
}
//排序 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstdlib>
using namespace std; long long int a[]; int main()
{
int n;
while (scanf("%d",&n) && n)
{
for (int i = ; i<n; i++)
scanf("%lld",&a[i]);
sort(a, a+n);
int i;
for (i = ; i<n; i++)
{
if (a[i] != a[i-] && a[i] != a[i+])
break;
}
printf("%lld\n",a[i]);
}
return ;
}
#include <math.h>
#include <string.h>
#include <algorithm>
#include <stdio.h>
long a[];
int main()
{ memset(a,,sizeof(a));
long n;
while(scanf("%ld",&n)!=EOF&&n!=)
{
long long m;
for(int i=;i<n;i++)
{
scanf("%lld",&m);
a[m]+=;
}
for(int i=;i<;i++)
{
if(a[i]==)
{
printf("%lld\n",i);
}
}
}
return ;
}
其实,这题还有个更好的方法————位异或。
我们先了解一下位异或的运算法则吧:
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 = ;
while(n--)
{
scanf("%d",&x);
ans ^= x;
}
printf("%d\n",ans);
}
return ;
}
find your present (2) 2095的更多相关文章
- HDU 2095 find your present (2)
HDU 2095 find your present (2) 解法一:使用set 利用set,由于只有一个为奇数次,对一个数进行查询,不在集合中则插入,在集合中则删除,最后剩下的就是结果 /* HDU ...
- find your present (2) hdoj 2095
/* author:谦智 find your present (2) hdoj 2095 法一:用暴力 法二:用map 法三: 符号是^. 异或是个位运算符号,具体是怎么操作的请百度,这里有个特性使得 ...
- 杭电 2095 find your present (2)【位运算 异或】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2095 解题思路:因为只有我们要求的那个数出现的次数为奇数,所以可以用位运算来做,两次异或同一个数最后结 ...
- 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个数中只有一种数出现奇数次,其他全部出现偶数次,让你找到奇数次这个数 思路:简单异或运算题 /*********************************** ...
- HDOJ 2095
find your present (2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/1024 K (Java/Oth ...
- HDU.2095(异或运算)
find your present (2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
随机推荐
- 三——第二部分——第二篇论文 计划建设SQL Server镜像
本文接着前面的章节:SQL Server镜像简单介绍 本文出处:http://blog.csdn.net/dba_huangzj/article/details/27203053 俗话说:工欲善其事必 ...
- hdu 1429 胜利大逃亡(延续)(BFS+比特压缩)
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm& ...
- HDU-2857-Mirror and Light(计算几何)
Problem Description The light travels in a straight line and always goes in the minimal path between ...
- [Ruby]How to create singleton class ?
Singleton is one design pattern in the software engineering. Ruby has its own special feature to dec ...
- 黑马day11 脏读数据&解
数据库: create table account ( id int primary key auto_increment, name varchar(20), money double ); ins ...
- Block学习一门:基本使用,使用block包NSURLRequest异步请求
首先,看一下下面的代码: void (^myFirstBlock)(int theOne,int theTwo) = ^(int theOne,int theTwo){ NSLog(@"== ...
- UVA 1291 Dance Dance Revolution(DP)
意甲冠军:跳舞机有一个上5积分,分别central, top, bottom, left, right分,区区足站立还是需要1点物理,从一个单纯的脚central点上须要2点体力,从一个点上移动到相邻 ...
- poj 3013 Big Christmas Tree (dij+优先级队列优化 求最短)
模板 意甲冠军:给你一个图,1始终根,每一方都有单价值,每个点都有权重新. 每个边缘的价格值 = sum(后继结点重)*单价方值. 最低价格要求树值,它构成了一棵树n-1条边的最小价值. 算法: 1. ...
- 终端查询数据库sqlite(创建你自己,或者是coredata创建)那里的东西
首先需要知道数据库的路径,coredata一般都是在沙箱中创建Documents目录的.虽然他建立了自己看, sqlite3 #数据库路径# //进入数据库 .tables //查看数 ...
- 查询系统--基于Solr4.9.0实现
为什么非要搜索系统 随着在产品的数量的增长.和复杂的检索要求,直接从数据库中检索信息,它已经无法满足展示机搜索需求. 实例: keyword=%E8%8B%B9%E6%9E%9C&enc=ut ...