您的位置:首页 > 其它

PAT-A1144 The Missing Number 题目内容及题解

2019-02-12 09:08 267 查看

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

题目大意

题目给定若干个整数,要求找到并输出不在给定序列中的最小的正整数。

解题思路

  1. 采用打表法记录在最小和最大范围内出现的数字;
  2. 从1开始遍历散列表,找到第一个散列表数值为0的数字时输出并跳出循环;
  3. 返回零值。

代码

[code]#include<stdio.h>
#define maxn 100010
int arr[maxn];

int main(){
int i,N,a;
scanf("%d",&N);
while(N--){
scanf("%d",&a);
if(a>=0&&a<maxn){
arr[a]=1;
}
}
for(i=1;1;i++){
if(arr[i]==0){
printf("%d\n",i);
return 0;
}
}
}

运行结果

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: