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 (≤105). 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

题意:

给出N个数,判断在这N个数中确实的最小的正整数.

思路:

因为整数是从1开始的,所以缺失的整数最大为N+1,开始的时候将vector数组初始化为-1,给出的数字为正数且小于等于N时,令v[N] = 1.最后遍历V数组,输出第一个遇到的-1的下标.若整个V数组都为1,则输出N+1.

Code:

#include <iostream>
#include <vector> using namespace std; int main() {
int n, t;
cin >> n; vector<int> v(n + 1, -1);
for (int i = 0; i < n; ++i) {
cin >> t;
if (t > 0 && t <= n) v[t] = 1;
}
bool found = false;
for (int i = 1; i <= n; ++i) {
if (v[i] < 0) {
cout << i << endl;
found = true;
break;
}
}
if (!found) cout << n + 1 << endl; return 0;
}

1144 The Missing Number的更多相关文章

  1. PAT 1144 The Missing Number

    1144 The Missing Number (20 分)   Given N integers, you are supposed to find the smallest positive in ...

  2. PAT 1144 The Missing Number[简单]

    1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...

  3. [PAT] 1144 The Missing Number(20 分)

    1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...

  4. PAT 甲级 1144 The Missing Number (20 分)(简单,最后一个测试点没过由于开的数组没必要大于N)

    1144 The Missing Number (20 分)   Given N integers, you are supposed to find the smallest positive in ...

  5. PAT(A) 1144 The Missing Number(C)统计

    题目链接:1144 The Missing Number (20 point(s)) Description Given N integers, you are supposed to find th ...

  6. PAT 甲级 1144 The Missing Number

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343463260160 Given N integers, you ...

  7. 1144 The Missing Number (20 分)

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

  8. 1144. The Missing Number (20)

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

  9. Leetcode-268 Missing Number

    #268.  Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...

随机推荐

  1. 看完我的笔记不懂也会懂----MongoDB

    MongoDb数据库学习 - 数据库的分类 - 数据库基本概念 - MongoDB常用指令 - MongoDB的CURD - sort({key:*[1,-1]}).limit(num).skip(n ...

  2. 微信小程序弹出层

    1.消息提示     wx.showToast wx.showToast({ title: '成功', icon: 'success', duration: 2000 })2.模态弹窗 wx.show ...

  3. c#(NPOI)DataTable导出execl,execl(支持解析公式)导入DataTable

    NPOI(C#)DataTable导出execl using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserMod ...

  4. AI数学基础之:概率和上帝视角

    目录 简介 蒙题霍尔问题 上帝视角解决概率问题 上帝视角的好处 简介 天要下雨,娘要嫁人.虽然我们不能控制未来的走向,但是可以一定程度上预测为来事情发生的可能性.而这种可能性就叫做概率.什么是概率呢? ...

  5. SHELL编程入门简介

    一.SHELL软件概念和应用场景 1) 学习Linux技术,不是为了学习系统安装.命令操作.用户权限.配置IP.网络管理,学习Linux技术重点:基于Linux系统部署和维护各种应用软件.程序(Apa ...

  6. JVM笔记 -- JVM经历了什么?

    Sun Classic VM 世界上第一款商用 Java 虚拟机,JDK1.4 已经淘汰. 内部只有解释器,可以自己外挂JIT编译器,但是二者只能使用其一,不能配合工作. hotspot 内置了该虚拟 ...

  7. 使用自定义注解和切面AOP实现Java程序增强

    1.注解介绍 1.1注解的本质 Oracle官方对注解的定义为: Annotations, a form of metadata, provide data about a program that ...

  8. 读 Kafka 源码写优雅业务代码:配置类

    这个 Kafka 的专题,我会从系统整体架构,设计到代码落地.和大家一起杠源码,学技巧,涨知识.希望大家持续关注一起见证成长! 我相信:技术的道路,十年如一日!十年磨一剑! 往期文章 Kafka 探险 ...

  9. Webpack 学习笔记(1) 开始

    目录 参考资料 1. 基础设定 2. 创建一个包 3. 使用配置文件完成打包命令 4. 使用 NPM Scripts 完成打包命令 参考资料 Getting Started | Webpack web ...

  10. 基金 A 类和 C 类、ETF、LOF、QDII 到底是啥?

    ETF 对于初入股市的新手来说,买了一只公司股票容易,想买一个行业的股票就不是很容易了. 比如你要懂得行业里都有谁,每个公司分配多少钱,最主要股票交易最少要交易 1 手也就是 100 股,要是想配置一 ...