前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【PAT甲级】The Missing Number

【PAT甲级】The Missing Number

作者头像
喜欢ctrl的cxk
发布2019-11-08 14:21:22
6710
发布2019-11-08 14:21:22
举报
文章被收录于专栏:Don的成长史Don的成长史

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/weixin_42449444/article/details/89448094

Problem Description:

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:

代码语言:javascript
复制
10
5 -25 9 6 1 3 4 2 5 17

Sample Output:

代码语言:javascript
复制
7

解题思路:

肯定是先要无脑set来记录所有出现在输入列表中的正整数啊,然后再无脑for循环找出第一个(也就是最小的那个)没有出现在set中的正整数即可。

AC代码:

代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int N;
    cin >> N;
    set<int> s;  //用来存放出现过的正整数
    for(int i = 0; i < N; i++)
    {
        int temp;
        cin >> temp;
        if(temp > 0)  //向set中插入正整数
        {
            s.insert(temp);
        }
    }
    int ans;   //第一个没出现过的正整数ans
    for(int i = 1; ; i++)
    {
        if(s.count(i) == 0)  //找到第一个没出现过的正整数
        {
            ans = i;
            break;
        }
    }
    cout << ans << endl;
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-04-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem Description:
  • Input Specification:
  • Output Specification:
  • Sample Input:
  • Sample Output:
  • 解题思路:
  • AC代码:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档