PAT 甲级 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

这道题和前一道题目一样,测试用例并没有开到int那么大,我把大于maxn的直接抛弃。

#include 
#include 
using namespace std;
const int maxn=100005;
bool visit[maxn]={false};
int main()
{
    int n,a;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a);
        if(a>0&&a<maxn)
        visit[a]=true;
    }
    int i=1;
    while(true)
    {
        if(visit[i])
            i++;
        else
        {
            printf("%d\n",i);
            break;
        }
    }
    return 0;
}


你可能感兴趣的:(PAT,OJ试题)