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开始遍历散列表,找到第一个散列表数值为0的数字时输出并跳出循环;
- 返回零值。
代码
[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; } } }
运行结果
相关文章推荐
- PAT 1144 The Missing Number (20 分)
- PAT 1144 The Missing Number python解法
- PAT (Advanced Level)-1144 The Missing Number
- PAT Advanced 1144. The Missing Number (20)
- PAT-A1151 LCA in a Binary Tree 题目内容及题解
- PAT-A1152/B1094 Google Recruitment/谷歌的招聘 题目内容及题解
- PAT-A1153/B1095 Decode Registration Card of PAT/解码PAT准考证 题目内容及题解
- PAT-A1154 Vertex Coloring 题目内容及题解
- PAT-A1145 Hashing - Average Search Time 题目内容及题解
- PAT-A1146 Topological Order 题目内容及题解
- PAT-A1147 Heaps 题目内容及题解
- PAT-A1148/B1089 Werewolf - Simple Version/狼人杀-简单版 题目内容及题解
- 1144. The Missing Number (20)
- PAT-A1150 Travelling Salesman Problem 题目内容及题解
- 1144. The Missing Number (20)
- PAT题解——1012. The Best Rank (25)
- HDU 1492 The number of divisors(约数) about Humble Numbers(数论题目要知道定理呀....)
- pat 1038 Recover the Smallest Number
- In company code XX, the number range 01 is missing for the year XXXX
- PAT 1049 Recover the Smallest Number (30)