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 ...
随机推荐
- css实现圆角三角形例子(无图片)
css实现圆角三角形例子(无图片) 以前我们做圆角时都会要用到图片来实现,今天我给大家整理的这款css圆角效果是不需要图片的纯css实现的哦,下面我们一起来看看吧. 效果如下图所示 三角形所对方向&q ...
- 关于为空必填js判断
为了减少一不必要的if逻辑判断,自已写了一个方法 $(function () { $("#btnAdd").click(function () { var strLinValu = ...
- C++ 引用做左值
//引用做左值 #include<iostream> using namespace std; int SetA(int *p){ *p = ; return *p; } int& ...
- DWZ(二):经常使用组件了解
上篇博客中我们大致明确了DWZ框架.以及它的一些优势,接下来的这篇博客是具体介绍了DWZ框架中一些经常使用组件的使用. 经常使用组件列表: Ajax 链接扩展 ...
- install phalcon on mac with XAMPP
首先使用brew安装对应版本的phalcon,参考https://docs.phalconphp.com/en/latest/reference/tutorial.html 安装好后会有 phalco ...
- Libcap的简介及安装
Libpcap 简介 libpcap 是unix/linux 平台下的网络数据包捕获函数包, 大多数网络监控软件都以它为基础. Libpcap 可以在绝大多数类unix 平台下工作. Libpcap ...
- ios应用, 设置不自己主动备份到iCloud
原创文章,转载请注明出处 ios项目,假设有内置下载或者程序动态生成文件的话,就要注意所下载或生成的文件,要不要自己主动备份到iCloud 假设没有合适的理由就自己主动上传大文件的话,可能在不能通过应 ...
- Android弹出一项权限请求
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(ena ...
- Linux命令之乐--script和scriptplay
script和scriptplay可以把终端会话记录到一个文件中,可以用来制作命令行教学视屏. 开始录制会话 [root@new test]# script -t >timing.log -a ...
- Eclipse配置Tomcat并运行
这篇文章介绍Eclipse配置tomcat.我们假设已经安装好JDK并且配置好了JDK的环境变量.然后我们需要下载并安装Eclipse和tomcat:Eclipse:http://www.eclips ...