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 ...
随机推荐
- c/c++学习之c++ 中的list <>容器
http://blog.csdn.net/mazidao2008/article/details/4802617 push 实例化 即添加 http://www.cnblogs.com/BeyondA ...
- backbone.js初探(转)
BackBone是JavaScript frameworks for creating MVC-like web applications,最近流行的用来建立单页面web application的工具 ...
- php -- PDO异常处理
异常处理: PHP:默认为直接报错 MYSQL:默认为静默模式,错就错,不报错 PDO:默认为静默模式,错就错,不报错 以前,当PHP碰到错误的时候,会直接报错,错误处理会变得相当麻烦.后来,当错误发 ...
- 【剑指offer】翻转单词顺序
转载请注明出处:http://blog.csdn.net/ns_code/article/details/27372033 题目描写叙述: JOBDU近期来了一个新员工Fish,每天早晨总是会拿着一本 ...
- Thinkphp新增字段无法插入到数据库问题
Thinkphp框架开发过程中,因需求需要改动数据表,新增了几个字段. 调用 M(‘xxx’)->add($data) 插入值时,新增的字段数据总是插入不进去,每次都是默认的值,于是恍然—-缓存 ...
- oracle和SQLserver数据库中select into 的区别
在Oracle中是这样的 在SQLserver中是这样的
- Gradle学习系列
http://www.cnblogs.com/davenkin/p/gradle-learning-1.html
- Spring配置文件总结
http://blog.csdn.net/zhejingyuan/article/details/41042789
- 小谈js原型链和继承
原型(prototype)在js中可是担当着举足轻重的作用,原型的实现则是在原型链的基础上,理解原型链的原理后,对原型的使用会更加自如,也能体会到js语言的魅力. 本文章会涉及的内容 原型及原型对象 ...
- java利用反射机制判断对象的属性是否为空以及获取和设置该属性的值
1.java利用反射机制判断对象的属性是否为空: Map<String,String> validateMap = new LinkedHashMap<String, String& ...