PAT 1144 The Missing Number

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

#include<iostream>
#include<vector>
#include<set>
using namespace std;
int main(){
  int n, t, cnt=1;
  cin>>n;
  set<int> s;
  for(int i=0; i<n; i++){
    cin>>t;
    if(t>0)
      s.insert(t);
  }
  auto it=s.begin();
  for(; it!=s.end(); it++){
    if(cnt++!=*it)
      break;
  }
  it==s.end()?cout<<cnt<<endl:cout<<cnt-1<<endl;
  return 0;
}

原文地址:https://www.cnblogs.com/A-Little-Nut/p/9652290.html

时间: 2024-07-31 07:59:48

PAT 1144 The Missing Number的相关文章

1144 The Missing Number (20 分)

1144 The Missing Number (20 分) 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

PAT 甲级 1144 The Missing Number

https://pintia.cn/problem-sets/994805342720868352/problems/994805343463260160 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 eac

PAT Advanced 1144 The Missing Number (20分)

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 giv

LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number

数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. (Easy) 分析:求n的阶乘中末位0的个数,也就是求n!中因数5的个数(2比5多),简单思路是遍历一遍,对于每个数,以此除以5求其因数5的个数,但会超时. 考虑到一个数n比他小

leetcode Missing Number

题目连接 https://leetcode.com/problems/missing-number/ Missing Number Description Given an array containing n distinct numbers taken from$ 0, 1, 2, ..., n$, find the one that is missing from the array. For example,Given nums = $[0, 1, 3]$ return $2$. Not

hdu 5166 Missing number

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5166 Missing number Description There is a permutation without two numbers in it, and now you know what numbers the permutation has. Please find the two numbers it lose. Input There is a number T shows t

【LeetCode】268. Missing Number

Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example,Given nums = [0, 1, 3] return 2. Note:Your algorithm should run in linear runtime complexity. Could you i

&lt;LeetCode OJ&gt;Missing Number【268】

268. Missing Number My Submissions Question Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example, Given num

HDU - 5166 - Missing number &amp;&amp; 5167 - Fibonacci

Missing number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 430    Accepted Submission(s): 233 Problem Description There is a permutation without two numbers in it, and now you know what num