博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pta 1144 The Missing Number
阅读量:4608 次
发布时间:2019-06-09

本文共 1239 字,大约阅读时间需要 4 分钟。

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:

105 -25 9 6 1 3 4 2 5 17

Sample Output:

7

题目大意:给定n个数,找出这n个数里丢失的最小的正整数

注意细节:

(1)可能n个数全是负数,输出1

(2)正整数要从1开始记录,数中可能存在重复的数

(3)可能n个数都是连续的正整数,且从1开始,那么输出最大的数+1

代码如下:

#include
using 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;}

 

转载于:https://www.cnblogs.com/lu1nacy/p/10016628.html

你可能感兴趣的文章
软件工程第一次作业补充
查看>>
N76E003---输入捕获
查看>>
poj 1094 Sorting It All Out(拓扑排序)
查看>>
acdream B - 郭式树 (水题 卡cin,cout, 卡LL)
查看>>
BMP图像格式
查看>>
python的匿名函数lambda解释及用法
查看>>
c#遍历Dictionary使用KeyValuePair
查看>>
defineProperties属性的运用==数据绑定
查看>>
关于 IOS 发布的点点滴滴记录(一)
查看>>
《EMCAScript6入门》读书笔记——14.Promise对象
查看>>
CSS——水平/垂直居中
查看>>
Eclipse连接mysql数据库jdbc下载(图文)
查看>>
Python中Selenium的使用方法
查看>>
三月23日测试Fiddler
查看>>
20171013_数据库新环境后期操作
查看>>
poj 1654 && poj 1675
查看>>
运维派 企业面试题1 监控MySQL主从同步是否异常
查看>>
Docker 版本
查看>>
poj 1753 Flip Game
查看>>
在深信服实习是怎样的体验(研发测试岗)
查看>>