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 (≤10​5​​). 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[简单]的更多相关文章

  1. PAT 1144 The Missing Number

    1144 The Missing Number (20 分)   Given N integers, you are supposed to find the smallest positive in ...

  2. [PAT] 1144 The Missing Number(20 分)

    1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...

  3. PAT 甲级 1144 The Missing Number (20 分)(简单,最后一个测试点没过由于开的数组没必要大于N)

    1144 The Missing Number (20 分)   Given N integers, you are supposed to find the smallest positive in ...

  4. PAT(A) 1144 The Missing Number(C)统计

    题目链接:1144 The Missing Number (20 point(s)) Description Given N integers, you are supposed to find th ...

  5. HDU 5166 Missing number 简单数论

    Missing number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) [ ...

  6. PAT 甲级 1144 The Missing Number

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343463260160 Given N integers, you ...

  7. PAT 1019 General Palindromic Number[简单]

    1019 General Palindromic Number (20)(20 分) A number that will be the same when it is written forward ...

  8. 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 ...

  9. 1144 The Missing Number (20 分)

    Given N integers, you are supposed to find the smallest positive integer that is NOT in the given li ...

随机推荐

  1. 关于ie中实现弹性盒模型-我的css

    css3中的弹性盒模型大家都不陌生,但是能否在ie6中实现呢?第三方库中涉及到的页少之又少,也有一部分css框架中支持各种布局,下面给出我用的盒模型样式(为了以后copy方便而已): /******* ...

  2. iOS 创建本地私有库 保存功能代码

    创建本地私有库 >>> cd /Users/cxx/Desktop/Mange_JJH/Lib >>> pod lib create TZTools >> ...

  3. php队列算法[转]

    <?php/*** php队列算法* * Create On 2010-6-4* Author Been* QQ:281443751* Email:binbin1129@126.com**/cl ...

  4. 打印系统所有的PID

    #!/usr/bin/env python #-*- coding:utf-8 -*- ''' 打印系统所有的PID ''' import os def get_all_pid(): for pid ...

  5. 数据库为什么要用B+树结构--MySQL索引结构的实现(转)

    B+树在数据库中的应用 { 为什么使用B+树?言简意赅,就是因为: 1.文件很大,不可能全部存储在内存中,故要存储到磁盘上 2.索引的结构组织要尽量减少查找过程中磁盘I/O的存取次数(为什么使用B-/ ...

  6. dubbo项目实战代码展示

    最近公司项目使用dubbo服务,于是就去网上搜索关于dubbo的相关资料,真的很多,但是对于很多人并不是很了解框架或者 不是太适合新手的片段代码,于是我就根据项目的相关内容把dubbo部分单独切出来, ...

  7. 【IIS】IIS 7.0/7.5 无法启动 w3svc 服务

    一般情况下,window IIS安装完毕后,会启动C:\inetpub\ 产生 类似C:\inetpub\temp\apppools的文件夹,如果IIS被改动过,此文件夹不会自动生成.需要手动添加. ...

  8. EXSI5.5以上开启KVM二次虚拟化

    1,在EXSI5.5上创建centos虚拟机(过程省略) 2,开启EXSI5.5的ssh功能 3,ssh登录虚拟机 4,编辑虚拟配置文件 /vmfs/volumes/datastore1/目录下,找到 ...

  9. echart绑定点击事件

    实例页面:http://echarts.baidu.com/echarts2/doc/example/event.html option = { tooltip : { trigger: 'axis' ...

  10. 演示一个简单的Redis队列

    0.Windows Service版下载 https://github.com/rgl/redis/downloads 1.新建一个Console项目 打开Nuget控制台,执行以下命令 Instal ...