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

2021/8/19 6:08:55

本文主要是介绍PAT (Advanced Level) Practice 1144 The Missing Number (20 分) 凌宸1642,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

题目描述:

Given N integers, you are supposed to find the smallest positive integer that is NOT in the given list.

译:给定 N 个整数,您应该找到不在给定列表中的最小正整数。


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.

译:每个输入文件包含一个测试用例。 对于每种情况,第一行给出一个正整数 N (≤105)。 然后在下一行给出 N 个整数,用空格分隔。 所有数字都在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

The Idea:

  • 对输入的正数进行标记,标记其存在。
  • 依次遍历,第一个没被标记的正数就是答案。

The Codes:

#include<bits/stdc++.h>
using namespace std ;
const int maxn = 100010 ;
bool exist[maxn] = {false} ; 
int main(){
	int n , t ;
	cin >> n ;
	for(int i = 0 ; i < n ; i ++){
		cin >> t ;
		if(t > 0 && t < maxn) exist[t] = true ; 
	}
	for(int i = 1 ; i < maxn ; i ++){
		if(!exist[i]){
			cout << i << endl ;
			break ;
		}
	}
	return 0 ;
}



这篇关于PAT (Advanced Level) Practice 1144 The Missing Number (20 分) 凌宸1642的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程