PAT (Advanced Level) Practice 1144 The Missing Number (20分)

1.题目

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

2.代码

#include<iostream>
#include<cstdio>
#include<unordered_map>
using namespace std;
int main()
{
	int n, t;
	unordered_map<int,int>list;
	scanf("%d", &n);
	for (int i = 0; i<n; i++)
	{
		scanf("%d", &t);
		if (t>0)
			list[t]++;
	}
	for (int i = 1; i<100002; i++)
	{
		if (list[i] == 0)
		{
			printf("%d\n", i);
			break;
		}
	}


}

 

posted @ 2020-03-20 08:48  Jason66661010  阅读(71)  评论(0编辑  收藏  举报