PAT 1144 The Missing Number[简单]
1144 The Missing Number(20 分)
Given N integers, you are supposed to find the smallest positive integer that is NOT in the given list.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤105). Then N integers are given in the next line, separated by spaces. All the numbers are in the range of int.
Output Specification:
Print in a line the smallest positive integer that is missing from the input list.
Sample Input:
10
5 -25 9 6 1 3 4 2 5 17
Sample Output:
7
题目大意:给了N个整数,找到不在其中的最小的正整数。
//猛一看感觉很简单,第一次提交,有3个测试点没通过:
#include <iostream>
#include <cstdio>
#include <map>
using namespace std; map<int,int>mp;
int main()
{
int n,temp;
cin>>n;
for(int i=;i<n;i++){
cin>>temp;
if(temp<&&mp[-temp]!=)
mp[-temp]=;//2表示当前以负数形式出现。
else
mp[temp]=;
}
for(int i=;i<=n;i++){
if(mp[i]==||mp[i]==){
cout<<i;break;
}
}
return ;
}
//利用map的按照关键字自排序特性,写成了这样,还是2,3,5测试点过不去,想不出来哪里错了。
#include <iostream>
#include <cstdio>
#include <map>
using namespace std; map<int,int>mp;
int main()
{
int n,temp;
cin>>n;
for(int i=;i<n;i++){
cin>>temp;
if(temp<&&mp[-temp]!=)
mp[-temp]=;//2表示当前以负数形式出现。
else
mp[temp]=;
}
// for(int i=1;i<=n;i++){
// if(mp[i]==2||mp[i]==0){
// cout<<i;break;
// }
// }
int ct=;
for(auto it=mp.begin();it!=mp.end();it++){
//cout<<it->first<<" "<<it->second<<'\n';
if(it->first==ct&&it->second!=)ct++;//按照自排序特性,判断是否相等。
else{
cout<<ct;break;
}
}
return ;
}
代码转自:https://www.liuchuo.net/archives/4662
#include <iostream>
#include <map>
using namespace std;
int main() {
int n, a, num = ;
cin >> n;
map<int, int> m;
for (int i = ; i < n; i++) {
cin >> a;
m[a]++;
}
while(++num)
if (m[num] == ) break;
cout << num;
return ;
}
//看完这个我才反应过来,map的关键字可以是负数的,又不是数组下标,你那么谨慎干什么。。
#include <iostream>
#include <cstdio>
#include <map>
using namespace std; map<int,int>mp;
int main()
{
int n,temp;
cin>>n;
for(int i=;i<n;i++){
cin>>temp;
mp[temp]=;
}
//for(int i=1)//这里最好别用for循环,就while循环就可以,因为不太好控制上限,
//有可能是int的最大值呢啊
int num=;
while(++num){//这里真的还是++num最好,num++都需要-1的
//很少用++num,学习了。
if(mp[num]==){
cout<<num;break;
}
}
return ;
}
//学习了!
PAT 1144 The Missing Number[简单]的更多相关文章
- PAT 1144 The Missing Number
1144 The Missing Number (20 分) Given N integers, you are supposed to find the smallest positive in ...
- [PAT] 1144 The Missing Number(20 分)
1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...
- PAT 甲级 1144 The Missing Number (20 分)(简单,最后一个测试点没过由于开的数组没必要大于N)
1144 The Missing Number (20 分) Given N integers, you are supposed to find the smallest positive in ...
- PAT(A) 1144 The Missing Number(C)统计
题目链接:1144 The Missing Number (20 point(s)) Description Given N integers, you are supposed to find th ...
- HDU 5166 Missing number 简单数论
Missing number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) [ ...
- PAT 甲级 1144 The Missing Number
https://pintia.cn/problem-sets/994805342720868352/problems/994805343463260160 Given N integers, you ...
- PAT 1019 General Palindromic Number[简单]
1019 General Palindromic Number (20)(20 分) A number that will be the same when it is written forward ...
- PAT A1144 The Missing Number (20 分)——set
Given N integers, you are supposed to find the smallest positive integer that is NOT in the given li ...
- 1144 The Missing Number (20 分)
Given N integers, you are supposed to find the smallest positive integer that is NOT in the given li ...
随机推荐
- 关于SQL高量问题
一工作今天在用DataTable.Table.Select("字段 like")查询时候老是碰到格式不正确 dtrFoundRow = dtvOOView.Table.Select ...
- 一种基于匹配回朔的 css3 选择器引擎实现
介绍 CSS 选择器是一种应用于 DOM 节点查找场景的特定微型语法, 本质上和正则表达式一样都是一种模式匹配语言,灵活使用可以方便得获取指定位置的节点集合. 目前 W3C 推荐标准为 Selecto ...
- could not find com.android.support.appcompat-v7:23.4.0
导入别人的工程到AS中,出现错误,是由于android studio的版本比所加载的工程所使用的版本低,有些包不是最新的. 我的android studio这个包的版本是 v7:23.1.1 所以需要 ...
- ftp安装和虚拟用户创建
安装 1.安装 sudo apt-get install vsftpd 2 查看安装结果 安装完毕,检查vsftpd进程是否已启动,可以查看进程或者查看监听端口 ps -eaf|grep vsftpd ...
- 关于Android Animation的setFillBefore、setFillAfter和setFillEnable
1. 如果是独立的Animation,只有setFillAfter有效,设置为true动画结束后保持最后的状态 2. 如果是AnimationSet中的Animation,因为Animation的作用 ...
- LA3485 Bridge[(辛普森自适应)微积分]
做此题完全是为了练积分. [普通求导版] Select Code #include<cstdio> #include<cmath> using namespace std; t ...
- 【BZOJ3881】[Coci2015]Divljak fail树+树链的并
[BZOJ3881][Coci2015]Divljak Description Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操 ...
- html的table列表根据奇数还是偶数行显示不同颜色
<tr <s:if test="#sts.even"> class="table_1" onMouseOut="this.class ...
- vue里监听安卓的物理返回键
Hybrid App中,原生内嵌H5单页,由于安卓是有物理返回键的,按安卓物理返回键的时候会返回到上一个路由. 实际中需求是:当有弹层的时候,按物理返回键是关闭弹层,是页面的时候才执行返回上一个路由, ...
- [Android Tips] 29. 如何判断当前编译的是哪个 Flavor ?
背景说明 应用需要针对不同的市场集成不同的第三方 SDK ,比如:面向海市场的版本需要集成 google-service apply plugin: 'com.google.gms.google-se ...