PTA The Missing Number(20分)

無限の光を放つのは人間の心であり、無限の闇を生み出すのも人間の心であり、光と闇が絡み合って戦うこの世界は、懐かしくて無力な世界です。

N個の整数が与えられると、与えられたリストにない最小の正の整数を見つけることになります。

入力仕様:

各入力ファイルには1つのテストケースが含まれています。いずれの場合も、最初の行は正の整数N(≤105)を示します。次に、スペースで区切られたN個の整数が次の行に表示されます。すべての数値はintの範囲です 

出力仕様:

入力リストから欠落している最小の正の整数を行に出力します。

入力例:

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

出力例:

7
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#include<climits>//INT_MAX
//#include<bits/stdc++.h>
#define PP pair<ll,int>
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fll
#define dinf 1000000000000.0
#define PI 3.1415926
typedef long long ll;
using namespace std;
int const mod=1e9+7;
const int maxn=1000100;
bool fg[maxn]={false};
int n,num;
int main(){
    scanf("%d",&n);
    for(int i=0; i<n; i++){
        scanf("%d",&num);
        if(num>0&&num<maxn)
            fg[num]=true;
    }
    for(int i=1; i<maxn; i++){
        if(fg[i]==false){
            printf("%d",i);
            return 0;
        }
    }
    return 0;
}

 

おすすめ

転載: blog.csdn.net/weixin_44170305/article/details/108526620