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 ...
随机推荐
- springboot启动抛出javax.websocket.server.ServerContainer not available
问题描述:spring boot接入websocket时,启动报错:javax.websocket.server.ServerContainer not available <dependenc ...
- cocos2dx创建工程
p.p1 { margin: 0; font: 17px "Helvetica Neue"; color: rgba(69, 69, 69, 1) } 官网镇楼: http://w ...
- linux的0,1,2号进程
一.答案 https://blog.csdn.net/gatieme/article/details/51532804 仔细读一下作者的博客,都是操作系统底层相关. 二.补充: 1.linux代码,a ...
- js中数据、内存、变量的概念及三者之间的关系
目录 数据.内存.变量的概念及三者之间的关系 什么是数据 数据的特点 什么是内存 栈内存 堆内存 JS引擎如何管理内存 什么是变量 变量是普通类型时 变量是引用类型时 数据.内存.变量的三者之间的关系 ...
- KDE 桌面不显示背景和状态栏
在arch的一次更新后,kde的桌面背景,插件,状态栏变成了一片黑,如下图所示. 解决办法:删除/home/stone/.config/plasma-org.kde.plasma.desktop-ap ...
- DNS Rebinding漏洞原理
目录 SSRF过滤器设计 背景知识 DNS TTL 公网DNS服务器 DNS重绑定 自建DNS服务器 利用步骤图解 实战中的注意事项 防御 参考 DNS Rebinding 广泛用于绕过同源策略.SS ...
- 《C++ Primer》笔记 第9章 顺序容器
顺序容器类型 类型 解释 vector 可变大小数组.支持快速随机访问.在尾部之外的位置插入或删除元素可能很慢 deque 双端队列.支持快速随机访问.在头尾位置插入.删除速度很快 list 双向链表 ...
- Hi3519 SDK搭建、问题总结及yolov3 RFCN的运行结果与测试
下面记录一下,在搭建Hi3519A SDK的注意事项与遇到的问题解决,及Hi3519A SDK环境下进行yolov3.RFCN的测试.(具体的Hi3519A的SDK环境搭建参考后面随笔-Hi3559A ...
- 基于dlib+django+python 实现web端人脸打卡
face_recognition 基于python+django+dlib实现的人脸打卡系统 开始之前 windows用户需要安装 VS2017 其他VS版本也行 linux用户需要安装c++编译器( ...
- 03-Spring默认标签解析
默认标签的解析 上一篇分析了整体的 xml 文件解析,形成 BeanDefinition 并注册到 IOC 容器中,但并没有详细的说明具体的解析,这一篇主要说一下 默认标签的解析,下一篇主要说自定义标 ...