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 ...
随机推荐
- springboot启动时过滤不需要注入的类
在springbootApplication启动类上加入注解 @ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterTy ...
- [JZOJ6353] 【NOIP2019模拟】给
题目 题目大意 对于所有的整数\(k \in [1,n]\),求叶子结点有\(k\)个的二叉树个数,满足每个非叶子结点都有两个儿子,并且对于每个叶子结点,从根节点到它经过的向左的边数少于等于\(m\) ...
- Delphi 字符串函数 StrUtils(大全)
引用单元: StrUtils; 首部 function AnsiResemblesText(const AText, AOther: string): Boolean; $[StrUtils.pas ...
- 云栖专辑|阿里开发者们的第二个感悟:PG大V德哥的使命感与开放心态
摘要: 2018年12月20日,云栖社区3岁.阿里巴巴常说“晴天修屋顶”,所以我们特别制作了这个专辑——分享给开发者们20个阿里故事,50本书籍. 2015年12月20日,云栖社区上线.2018年12 ...
- sql语句创建表
create table `search_custom_mall` ( `id` ) NOT NULL PRIMARY KEY AUTO_INCREMENT, `uid` ) NOT NULL, `n ...
- 最小表示法——牛客多校第七场A
脑瘫一样暴力,贪心找最小表示的串,判一个串是否是最小表示法时也是暴力地判.. 但是想不通复杂度是怎么算的.. #include<bits/stdc++.h> using namespace ...
- 其它课程中的python---2、NumPy模块
其它课程中的python---2.NumPy模块 一.总结 一句话总结: numpy在数组计算方面又快又方便 1.NumPy中的ndarray是一个多维数组对象,该对象由哪两部分组成? -实际的数据 ...
- Devexpress ProgressBarControl的使用
1 private void simpleButton1_Click(object sender, EventArgs e) 3 { 4 //设置一个最小值 5 progressBarControl1 ...
- jquery控件的学习
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- jmeter接口测试(基础)
一.jmeter创建请求 1.运行jmeter:jmeter.bat 2.右键测试计划,添加一个:线程组 3.右键前面添加的线程组,添加一个:http请求 4.填写请求信息: 5.右键线程组, ...