HDU-2095-find your present (2)-位或/STL(set)
InputThe 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.OutputFor each case, output an integer in a line, which is the card number of your present.Sample Input
5
1 1 3 2 2
3
1 2 1
0
Sample Output
3
2 use scanf to avoid Time Limit Exceeded
Hint
Hint 题意:
给出n,说明有n个数字,要求找出一个与其他数字不同的那个数,只有需要找的那个数字出现过奇数次,其他都是偶数次。
法一:
这是用容器写的,用了set
s.find(x)!=s.end() 说明找到了
s.erase(x); 找到和它一样的就把它删掉
s.insert(x); 没有找到就把这个数字插入
需要注意的是:题目说了,找到的那个数字是奇数个,而其他数字都是偶数个,所以可以利用set,否则要是我们需要找3,给出一组数据2、2、2、3像这样是找不出来的。
#include<iostream>
#include<iomanip>
#include<string.h>
#include<set>
#define inf 0x3f3f3f3f
using namespace std; int main()
{
std::ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int n;
while(cin>>n)
{
if(n==)
break;
set<int>s;
s.clear();
int x;
for(int i=;i<n;i++)
{
cin>>x;
if(s.find(x)!=s.end())//找到了
s.erase(x);//删掉
else//没有找到
s.insert(x);
}
cout<<*s.begin()<<endl;
}
return ;
}
法二:
利用位运算
#include<iostream>
#include<iomanip>
#include<string.h>
#include<set>
#define inf 0x3f3f3f3f
using namespace std; int main()
{
std::ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int n;
while(cin>>n)
{
if(n==)
break;
int x=;//0和任何非0数m异或都为0;
for(int i=;i<n;i++)
{
int ss;
cin>>ss;
x=x^ss;
}
cout<<x<<endl;
}
return ;
}
HDU-2095-find your present (2)-位或/STL(set)的更多相关文章
- HDU 2095 find your present (2)
HDU 2095 find your present (2) 解法一:使用set 利用set,由于只有一个为奇数次,对一个数进行查询,不在集合中则插入,在集合中则删除,最后剩下的就是结果 /* HDU ...
- 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 ...
- HDU 1004 Let the Balloon Rise【STL<map>】
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- 杭电 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 ...
随机推荐
- memset函数及其用法,C语言memset函数详解
在前面不止一次说过,定义变量时一定要进行初始化,尤其是数组和结构体这种占用内存大的数据结构.在使用数组的时候经常因为没有初始化而产生“烫烫烫烫烫烫”这样的野值,俗称“乱码”. 每种类型的变量都有各自的 ...
- node-express(1)建立post、get、跨域问题解决方案
首先下载express:npm i express let ess=require('express'); let app=ess(); let bodyParser=require('body-pa ...
- 使用 SpringBoot 配置发送邮件功能
1.使用 SpringBoot 配置发送邮件功能 项目总体结构 用户表设计 SET FOREIGN_KEY_CHECKS=0; CREATE DATABASE sample; USE sample; ...
- LoadRunner模拟REST接口的json请求
LoadRunner模拟REST接口的json请求 现在很多手机应用的性能测试,REST接口调用通过json格式,在用loadrunner模拟这些json请求时,需要开发提供 1.供接口地址 2.提交 ...
- html图片导入画布
首先定义一个画布 canvas id="myCanvas"></canvas> var canvas = document.getElementById('myC ...
- Python 列表,元组,字典
0)字符串切片 py_str = 'python' >>>py_str[0] #取第一个字符串,返回值为"p",超出范围会报错 >>>py_st ...
- NOIp2018集训test-9-1(pm)
欢乐%你赛,大家都AK了. 1. 小澳的方阵 吸取了前几天的教训,我一往复杂的什么二维树状数组上想就立刻打住阻止自己,就可以发现它是超级大水题了.记录每一行每一列最后一次的修改,对每个格子看它所在行和 ...
- NX二次开发-UF_ASSEM_ask_component_data获取装配部件的相关信息
NX9+VS2012 #include <uf.h> #include <uf_ui.h> #include <uf_assem.h> ], void* user_ ...
- NX二次开发-获取按钮的ID UF_MB_ask_button_id
NX9+VS2012 1.打开D:\Program Files\Siemens\NX 9.0\UGII\menus\ug_main.men 找到装配和PMI,在中间加上一段 TOGGLE_BUTTON ...
- c++ pb_ds库,实现 红黑树,Splay
C++ pb_ds库 #include <ext/pb_ds/assoc_container.hpp>#include <ext/pb_ds/tree_policy.hpp> ...