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:

  1. 10
  2. 5 -25 9 6 1 3 4 2 5 17

Sample Output:

  1. 7

题目大意:给了N个整数,找到不在其中的最小的正整数。

//猛一看感觉很简单,第一次提交,有3个测试点没通过:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <map>
  4. using namespace std;
  5.  
  6. map<int,int>mp;
  7. int main()
  8. {
  9. int n,temp;
  10. cin>>n;
  11. for(int i=;i<n;i++){
  12. cin>>temp;
  13. if(temp<&&mp[-temp]!=)
  14. mp[-temp]=;//2表示当前以负数形式出现。
  15. else
  16. mp[temp]=;
  17. }
  18. for(int i=;i<=n;i++){
  19. if(mp[i]==||mp[i]==){
  20. cout<<i;break;
  21. }
  22. }
  23. return ;
  24. }

//利用map的按照关键字自排序特性,写成了这样,还是2,3,5测试点过不去,想不出来哪里错了。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <map>
  4. using namespace std;
  5.  
  6. map<int,int>mp;
  7. int main()
  8. {
  9. int n,temp;
  10. cin>>n;
  11. for(int i=;i<n;i++){
  12. cin>>temp;
  13. if(temp<&&mp[-temp]!=)
  14. mp[-temp]=;//2表示当前以负数形式出现。
  15. else
  16. mp[temp]=;
  17. }
  18. // for(int i=1;i<=n;i++){
  19. // if(mp[i]==2||mp[i]==0){
  20. // cout<<i;break;
  21. // }
  22. // }
  23. int ct=;
  24. for(auto it=mp.begin();it!=mp.end();it++){
  25. //cout<<it->first<<" "<<it->second<<'\n';
  26. if(it->first==ct&&it->second!=)ct++;//按照自排序特性,判断是否相等。
  27. else{
  28. cout<<ct;break;
  29. }
  30. }
  31. return ;
  32. }

代码转自:https://www.liuchuo.net/archives/4662

  1. #include <iostream>
  2. #include <map>
  3. using namespace std;
  4. int main() {
  5. int n, a, num = ;
  6. cin >> n;
  7. map<int, int> m;
  8. for (int i = ; i < n; i++) {
  9. cin >> a;
  10. m[a]++;
  11. }
  12. while(++num)
  13. if (m[num] == ) break;
  14. cout << num;
  15. return ;
  16. }

//看完这个我才反应过来,map的关键字可以是负数的,又不是数组下标,你那么谨慎干什么。。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <map>
  4. using namespace std;
  5.  
  6. map<int,int>mp;
  7. int main()
  8. {
  9. int n,temp;
  10. cin>>n;
  11. for(int i=;i<n;i++){
  12. cin>>temp;
  13. mp[temp]=;
  14. }
  15. //for(int i=1)//这里最好别用for循环,就while循环就可以,因为不太好控制上限,
  16. //有可能是int的最大值呢啊
  17. int num=;
  18. while(++num){//这里真的还是++num最好,num++都需要-1的
  19. //很少用++num,学习了。
  20. if(mp[num]==){
  21. cout<<num;break;
  22. }
  23. }
  24. return ;
  25. }

//学习了!

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. 举例说明:Hadoop vs. NoSql vs. Sql vs. NewSql

    转自:http://blog.jobbole.com/86269/   尽管层次数据库如今在大型机上依然被广泛使用,但关系数据库(RDBMS)(SQL)已经占领了数据库市场,并且表现的相当优异.我们存 ...

  2. 如果将一个类设置为abstract,则此类必须被继承使用

    利用final定义方法:这样的方法为一个不可覆盖的方法. Public final void print(){}: 为了保证方法的一致性(即不被改变),可将方法用final定义. 如果在父类中有fin ...

  3. node 下好用的工具

    1. supervisor Node Supervisor is used to restart programs when they crash. Node Supervisor 是用来当程序崩溃时 ...

  4. C语言跳出循环

    使用while或for循环时,如果想提前结束循环(在不满足结束条件的情况下结束循环),可以使用break或continue关键字. break关键字 在<C语言switch语句>一节中,我 ...

  5. 开源内容管理系统Joomla3.5发布 基于PHP 7

    导读 作为深受广大站长喜爱的Joomla开源内容管理系统(Content Management System, CMS)正式推出3.5版本,这也是首个完全支持PHP 7语言开发的Joomla版本,基于 ...

  6. org.apache.hadoop.yarn.exceptions.InvalidAuxServiceException: The auxService: mapreduce_shuffle do

    在yarn-site.xml 配置文件中增加: <property> <name>yarn.nodemanager.aux-services</name> < ...

  7. Microsoft License Keys – Volume

    VLK Product Group Product KeyOffice XP Applications P3HBK-F86Y2-374PQ-KW92R-B36VTOffice 2003 Suites ...

  8. innerHTML 延后执行?

    时常会觉得 innerHTML 可能有延后执行的情况,比如下面代码: document.body.innerHTML = 'something'; alert('something else'); 明 ...

  9. 页面操作表单不会调用表单 value 属性的 set 函数

    在 ES5 通过 Object.defineProperty 数据绑定可以监听数据的变化,实现类似的效果,demo 执行如图: 但是这样把 表单元素的 value 属性设置为 访问器属性 后,有个问题 ...

  10. [Gradle] 针对不同的项目类型应用不同的 findbugs 配置

    build.gradle in project root subprojects { subProject -> plugins.withId('com.android.application' ...