1144 The Missing Number(20 分)

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:

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

Sample Output:

7

题意:给出n个数,输出不在给出的数中的最小正整数。
分析:1.用set去接收这些数(排序+去重)
题解:

#include<cstdlib>
#include<cstdio>
#include<set>
using namespace std;
int main() {
    int n;
    set<int> s;
    scanf("%d", &n);
    for (int i = 0; i != n; i++) {
        int t;
        scanf("%d", &t);
        s.insert(t);
    }
    int pre = *(s.begin());
    for (set<int>::iterator it = ++s.begin(); it != s.end(); it++) {
        if (*it - pre != 1 && pre+1 > 0) {
            printf("%d", pre + 1);
            return 0;
        }
        pre = *(it);
    }
    //如果遍历到最后一个仍然找不到符合要求的结果,
    //则输出最大的数之后的正整数(最大的数可能为正,或者仍为负)
    if (pre >= 0) {
        printf("%d", pre + 1);
    }
    else {
        printf("%d", 1);
    }
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 8,040评论 0 10
  • 深秋浅冬,雨一直淅淅沥沥的下着,微风拂面,略有一丝寒意。午后的我随意地走在家门口的公园里,满眼尽是那金黄金...
    小逆12阅读 631评论 1 2
  • 这是喜欢抱抱的Rachel第二次亲我的脸颊,很结实的吻,哈哈!和Rachel的按摩一样有力精准! 回想起来,第一次...
    Eelayne阅读 171评论 0 1
  • 剧情简介: 阿桑(罗仲谦 饰)是好友Terrance 家的专属司机;表面上循规蹈矩的他,却拥有不为人知的秘密。他一...
    janjin阅读 1,816评论 0 2
  • 我又不小心踢翻了吃剩的桶装泡面,汤汁流在地上,恶心的不得了,我赶紧拿纸巾擦干,却发现这些汤汁跟脏乱的地板居然是那么...
    Easy0425阅读 259评论 0 1

友情链接更多精彩内容