一、题目描述
Given N integers, you are supposed to find the smallest positive integer that is NOT in the given list.
Input Specification:
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
二、解题思路
找出一个序列中不存在的最小正数,常规思想肯定是建立一个bool数组标记每个数是否在序列内,随后遍历,但是要注意,输入的所有数的范围都是int范围内,而数组是无法开到那么大的,所以我这里用了一个unordered_map<int, int>标记每个数是否在序列中。
三、AC代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<unordered_map>
using namespace std;
unordered_map<int, int> mp;
int main()
{
int N, tmp;
scanf("%d", &N);
for(int i=0; i<N; i++)
{
scanf("%d", &tmp);
if(tmp > 0) mp[tmp] = 1;
}
int i=1;
while(1)
{
if(mp[i] == 0) break;
i++;
}
printf("%d", i);
return 0;
}