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 (≤105). 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:
105 -25 9 6 1 3 4 2 5 17
Sample Output:
7
题目大意:给定n个数,找出这n个数里丢失的最小的正整数
注意细节:
(1)可能n个数全是负数,输出1
(2)正整数要从1开始记录,数中可能存在重复的数
(3)可能n个数都是连续的正整数,且从1开始,那么输出最大的数+1
代码如下:
#includeusing namespace std;int a[1000002];int main(){ int n; while(cin >> n) { for(int i = 0;i < n;i ++) { cin >> a[i]; } sort(a,a + n); int i,fl = 0; for(i = 0;i < n - 1;i ++) { if(a[i] > 0) { if(a[i] == 1) fl = 1; if(fl) { if(a[i] == a[i + 1] || a[i + 1] == a[i] + 1) continue; else { cout << a[i] + 1 << endl; break; } } else { cout << fl + 1 << endl; break; } } } if(i == n - 1) { if(a[n - 1] > 0) cout << a[n - 1] + 1 << endl; else cout << 1 << endl; } } return 0;}