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
题目大意:
寻找第一个没有出现的正数。
题目思路:
题目最多给出100000个数,最多能覆盖1-100001。因此我们只需要对1-100001进行判断,如果有的数字不存在,则输出,如果都存在则100002一定不存在,输出100002即可。
#include <cstdio>
#include <cstring>
using namespace std;
int n, a, table[100002];
int main() {
memset(table, 0, sizeof(table));
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a);
if(a > 0 && a < 100002)table[a] = 1;
}
for (int i = 1; i <= 100001; i++) {
if (table[i] == 0) {
printf("%d\n", i);
return 0;
}
}
return 0;
}