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 ...
随机推荐
- 举例说明:Hadoop vs. NoSql vs. Sql vs. NewSql
转自:http://blog.jobbole.com/86269/ 尽管层次数据库如今在大型机上依然被广泛使用,但关系数据库(RDBMS)(SQL)已经占领了数据库市场,并且表现的相当优异.我们存 ...
- 如果将一个类设置为abstract,则此类必须被继承使用
利用final定义方法:这样的方法为一个不可覆盖的方法. Public final void print(){}: 为了保证方法的一致性(即不被改变),可将方法用final定义. 如果在父类中有fin ...
- node 下好用的工具
1. supervisor Node Supervisor is used to restart programs when they crash. Node Supervisor 是用来当程序崩溃时 ...
- C语言跳出循环
使用while或for循环时,如果想提前结束循环(在不满足结束条件的情况下结束循环),可以使用break或continue关键字. break关键字 在<C语言switch语句>一节中,我 ...
- 开源内容管理系统Joomla3.5发布 基于PHP 7
导读 作为深受广大站长喜爱的Joomla开源内容管理系统(Content Management System, CMS)正式推出3.5版本,这也是首个完全支持PHP 7语言开发的Joomla版本,基于 ...
- org.apache.hadoop.yarn.exceptions.InvalidAuxServiceException: The auxService: mapreduce_shuffle do
在yarn-site.xml 配置文件中增加: <property> <name>yarn.nodemanager.aux-services</name> < ...
- Microsoft License Keys – Volume
VLK Product Group Product KeyOffice XP Applications P3HBK-F86Y2-374PQ-KW92R-B36VTOffice 2003 Suites ...
- innerHTML 延后执行?
时常会觉得 innerHTML 可能有延后执行的情况,比如下面代码: document.body.innerHTML = 'something'; alert('something else'); 明 ...
- 页面操作表单不会调用表单 value 属性的 set 函数
在 ES5 通过 Object.defineProperty 数据绑定可以监听数据的变化,实现类似的效果,demo 执行如图: 但是这样把 表单元素的 value 属性设置为 访问器属性 后,有个问题 ...
- [Gradle] 针对不同的项目类型应用不同的 findbugs 配置
build.gradle in project root subprojects { subProject -> plugins.withId('com.android.application' ...