PAT_A1144#The Missing Number
Source:
Description:
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 (≤). 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
Keys:
- 散列(Hash)
Code:
1 /* 2 Data: 2019-05-23 19:50:36 3 Problem: PAT_A1144#The Missing Number 4 AC: 05:24 5 6 题目大意: 7 给定N(<=1e5)个整数(int范围内),找出不在表中的最小正数; 8 */ 9 10 #include<cstdio> 11 #include<map> 12 13 int main() 14 { 15 #ifdef ONLINE_JUDGE 16 #else 17 freopen("Test.txt", "r", stdin); 18 #endif 19 20 int n,x; 21 std::map<int,int> mp; 22 scanf("%d", &n); 23 for(int i=0; i<n; i++) 24 { 25 scanf("%d", &x); 26 mp[x]=1; 27 } 28 for(int i=1; i<1e9; i++) 29 { 30 if(mp[i]==0) 31 { 32 printf("%d\n", i); 33 break; 34 } 35 } 36 37 38 return 0; 39 }