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 ...
随机推荐
- thinkphp 上传安全
网站的上传功能也是一个非常容易被攻击的入口,所以对上传功能的安全检查是尤其必要的. 大理石平台支架 系统提供的上传类Think\Upload提供了安全方面的支持,包括对文件后缀.文件类型.文件大小以及 ...
- 【Codeforces 1148B】Born This Way
[链接] 我是链接,点我呀:) [题意] 有人要从A地飞向B地,然后从B地飞向C地. 给出A,B地的n.m个航班的出发时间. 已知从A到B的航班都是ta和tb时长到达B.C 只有到达B的时候航班还没起 ...
- java web 在tomcat没有正常输出
目录 文章背景 目录 问题介绍 问题解决 说明 参考文章 版本记录 文章背景 调试程序时候突然发现一些位置设置的日志输出没有了,最后总算是解决了! 目录 问题介绍 本地运行时候的环境如下: windo ...
- NX二次开发-UFUN单选菜单对话框uc1603
NX11+VS2013 #include <uf.h> #include <uf_ui.h> UF_initialize(); //单选菜单对话框 char sPromptSt ...
- fedora23上安装和运行MySQL server (MySQL 已经被MariaDB取代)
[root@localhost kemin]# dnf install mysql-server Fedora 23 - x86_64 - Updates ...
- JavaScript笔记 – 程序语法设计
一.基础语法设计 JavaScript是可以与HTML标记语言混合.用于网页交互式的解释型脚本语言.由国际标准ECMAScript提供核心语言功能.文档对象模型(DOM)提供访问和操作网页内容的方法和 ...
- SPI 及初始化例子
概述 时钟相性与极性 CPOL(Clock Polarity)控制空闲状态时SCK的值:CPOL=0,空闲时SCK=0:CPOL=1,空闲时SCK=1. CPHA(Clock Phase)控制何时捕获 ...
- Comet OJ - 2019 六一欢乐赛
传送门 #A: 思路:等差数列求和,看成俩次1+2+…+ n,多加的n减去,所以 ans = n*(n+1) - n. AC代码: #include<iostream> #include& ...
- 剑指offer——30包含min函数的栈
题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 题解: 借助辅助栈,新的数据<=f辅助栈顶时,就压入辅助栈,这样,就能保证辅 ...
- 本地 win7 与虚拟机Centos7 ping互通和Centos7 上网设置
VM 12 安装虚拟机我就不表示了 很简单网上找找 一 .虚拟机设置 1.修改使用了 VMWare 12 虚拟机,Oracle VM 用得有点晕 2.配置VM 的NET环境 3.在VMware虚拟机任 ...