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 ...
随机推荐
- GUI的广泛应用是当今计算机发展的重大成就之一
GUI的广泛应用是当今计算机发展的重大成就之一,它极大地方便了非专业用户的使用.人们从此不再需要死记硬背大量的命令,取而代之的是可以通过窗口.菜单.按键等方式来方便地进行操作.而嵌入式GUI具有下面几 ...
- JDBC中,用于表示数据库连接的对象是。(选择1项)
JDBC中,用于表示数据库连接的对象是.(选择1项) A.Statement B.Connection C. DriverManager D.PreparedStatement 解答:B
- mysql -- 按时间查询 今天、昨天、明天、上月....
今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 WHERE TO_DAYS( NO ...
- mysql根据查询结果,创建表
create table copy_materials_details (SELECT * FROM `materials_details`);
- strust2的Action中validateXxx方法的用法
Struts2控制部分时常需要验证来自页面的信息是否合法,若在执行struts2中 public String Xxx()方法操作数据库之前需要验证,ActionSupport提供了一个很好的方法.X ...
- WinDbg 解决Font.ToLogFont AccessViolationExcetion
有个程序总是在windows 2003 server 异常退出. 并且, 查看调用栈也肯奇怪, 应该是很正常的调用. 怀疑是堆溢出. 开启heap trace : C:\Program Files\ ...
- git chekout分支遇到问题:need merge
解决步骤: 在master上, 1.git add . 2.git commit 3.新建分支,并且checkout到此分支,重新提交
- 模板,BFS
#include <stdio.h> #include <string.h> #include <queue> using namespace std; struc ...
- 【BZOJ4881】5月月赛D 线段游戏 树状数组+set
Description quailty和tangjz正在玩一个关于线段的游戏.在平面上有n条线段,编号依次为1到n.其中第i条线段的两端点坐 标分别为(0,i)和(1,p_i),其中p_1,p_2,. ...
- PAT 甲级 1104 sum of Number Segments
1104. Sum of Number Segments (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Pen ...