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 ...
随机推荐
- statickeyword于C和C++用法
一.C语言statickeyword两个使用 1).一个功能修改内部使用的变量,函数内的静态变量.这些变量的寿命比功能不再,它是具有一定的函数"状态",使用静态变量的作用通常是不可 ...
- 更改Activity的最底层的布局
public void attachToActivity(Activity activity) { mActivity = activity; TypedArray a = activity.getT ...
- Spring Assert主张 (参议院检测工具的方法-主张)
Web 收到申请表格提交的数据后都需要对其进行合法性检查,假设表单数据是不合法的,该请求将被拒绝.分类似的,当我们写的类方法,该方法还经常需要组合成参 法国检查.假设参议院不符合要求,方法通过抛出异常 ...
- Codeforces 484E Sign on Fence(是持久的段树+二分法)
题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于 ...
- windows下一个erlang包装镜像启动
于linux环境,erlang经systools:make_script("",[])和systools:make_tar()命令生成图像包,安装镜像包,图片包的安装过程,通过替换 ...
- oracle 12c 三学习 pdb 可插拔测试
DECLARE l_result BOOLEAN; BEGIN l_result := DBMS_PDB.check_plug_compatibility( pdb_descr_file => ...
- Cocos2d-x 3.0final 终结者系列教程01-无论是从cocos2d-x2.x升级到版本cocos2d-x3.x
诡谲的江湖,易变. 花花世界,车来人往. 最终确定.安家,将Cocos2d-x3.0final相关技术精加工的版本.并推出了博客文章, 不为他人,只为自己. 学习交流QQ群:301954471 --- ...
- 【Android进阶】ZXing android 错误(Could not find class 'com.google.zxing.ResultPoint)
解决方法: 1.右键工程Build path, java build path,选择libraries 在右边的按钮中点击"Add Library" 选择"User li ...
- Swing开发界面时的一个bug复盘
问题:QA突然发个截图说一个Dialog上展示的东西变形了 分析:不理解,什么也没做,怎么会变形,刚刚我用的时候还正常.看看代码,的确什么也没更改:在本地测一下,也没有问题:baidu,bing,st ...
- Android ImageView分析并展开
版本号:1.0 日期:2014.6.11 2014.6.12 版权:© 2014 kince 转载注明出处 ImageView是开发中经常使用到的一个控件,也能够说是不可缺少的. 对于它的使用. ...