1144 The Missing Number
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的更多相关文章
- PAT 1144 The Missing Number
1144 The Missing Number (20 分) Given N integers, you are supposed to find the smallest positive in ...
- PAT 1144 The Missing Number[简单]
1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...
- [PAT] 1144 The Missing Number(20 分)
1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...
- PAT 甲级 1144 The Missing Number (20 分)(简单,最后一个测试点没过由于开的数组没必要大于N)
1144 The Missing Number (20 分) Given N integers, you are supposed to find the smallest positive in ...
- PAT(A) 1144 The Missing Number(C)统计
题目链接:1144 The Missing Number (20 point(s)) Description Given N integers, you are supposed to find th ...
- PAT 甲级 1144 The Missing Number
https://pintia.cn/problem-sets/994805342720868352/problems/994805343463260160 Given N integers, you ...
- 1144 The Missing Number (20 分)
Given N integers, you are supposed to find the smallest positive integer that is NOT in the given li ...
- 1144. The Missing Number (20)
Given N integers, you are supposed to find the smallest positive integer that is NOT in the given li ...
- Leetcode-268 Missing Number
#268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...
随机推荐
- 第48天学习打卡(CSS)
HTML + CSS +JavaScript 结构+表现+交互 HTML:结构 CSS:表现 JavaScript:交互 1什么是CSS 如何学习 1.CSS是什么 2.CSS怎么用(快速入门 ...
- vs调试qt代码,无法单步调试
在使用vs调试qt代码时,可以编译但无法单步调试QT源码.报错缺少qmain_win.cpp或者其他q******.cpp文件. 1.因为安装qt时没有安装qt源码库,重新下载一个src源码就可以了. ...
- SpringCloud(四):服务注册中心Eureka Eureka高可用集群搭建 Eureka自我保护机制
第四章:服务注册中心 Eureka 4-1. Eureka 注册中心高可用集群概述在微服务架构的这种分布式系统中,我们要充分考虑各个微服务组件的高可用性 问题,不能有单点故障,由于注册中心 eurek ...
- 使用Tomcat30分钟搭建个人服务器
目录 一.服务器简介 二.安装教程 三.出现的问题 一.服务器简介 Tomcat服务器是一个Apache软件资金会的开源项目,实现了Servlet,JSP,EL,WebSocket协议. 二.安装教程 ...
- HDOJ-3038(带权并查集)
How many answers wrong HDOJ-3038 一个很好的博客:https://www.cnblogs.com/liyinggang/p/5327055.html #include& ...
- Microsoft Teams 2021最新功能发布解读 – 会议篇
正在进行的2021年的Microsoft Ignite大会,发布了一系列跟Microsoft Teams相关的新功能,英文介绍请参考 https://techcommunity.microsoft.c ...
- 模式识别Pattern Recognition
双目摄像头,单目摄像头缺少深度 Train->test->train->test->predicive
- 怎样将大批量文件进行循环分组(reduce)?
背景 当有时候一个文件夹下有几万个几十万个文件时,我们的桌面终端打开这个文件夹可能会卡.或者将文件进行批量上传时,如果是在文件夹下全选,那么基本上浏览器就卡死了,当然也不能这样子操作滴~ 题主 ...
- Java 集合框架 01
集合框架· ArrayList 和 Vector 对象数组的概述和使用 * A:案例演示 * 需求:我有5个学生,请把这5个学生的信息存储到数组中,并遍历数组,获取到每个学生的信息 Student[] ...
- 2020年12月-第02阶段-前端基础-CSS Day03
CSS Day03 盒子模型(CSS重点) css学习三大重点: css 盒子模型 . 浮动 . 定位 主题思路: 理解: 1.能说出盒子模型有那四部分组成 2.能说出内边距的作用以及对盒子的影响 3 ...