Home PAT 1144 The Missing Number
Post
Cancel

PAT 1144 The Missing Number

Link

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
using namespace std;
int n;
int a[100010];
int main() {
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
        scanf("%d",&a[i]);
    sort(a+1,a+1+n);
    int j=1,ans=1;
    while(j<=n&&a[j]<=0) j++;
    if(j<=n){
        for(;j<=n;++j)
            if(a[j]-max(0,a[j-1])>1){ //注意这里,a[j-1]可能是负数,而比较的应该是非负数
                ans=max(0,a[j-1])+1;
                break;
            }
        if(j>n) ans=a[n]+1;
    }
    printf("%d\n",ans);
    return 0;
}
This post is licensed under CC BY 4.0 by the author.