1144 The Missing Number (20 分)【难度: 一般 / 知识点: 模拟】

在这里插入图片描述
https://pintia.cn/problem-sets/994805342720868352/problems/994805343463260160

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,x;
vector<int>ve;
int main(void)
{
    
    
    cin>>n;
    for(int i=0;i<n;i++)
    {
    
    
        cin>>x;
        if(x>0) ve.push_back(x);
    }
    sort(ve.begin(),ve.end());
    for(int i=0;i<ve.size();i++)
    {
    
    
        if(!i&&ve[i]>1)//如果第一个大于1
        {
    
    
            cout<<1;
            return 0;
        }
        if(i&&ve[i]-ve[i-1]>1)//如果两者的差大于1,说明中间少数了
        {
    
    
            cout<<ve[i-1]+1;
            return 0;
        }
    }
    if(!ve.size()) cout<<1;
    else cout<<ve[ve.size()-1]+1<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_46527915/article/details/121577189