PAT A1144 The Missing Number (20 分)——set
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
#include <stdio.h>
#include <algorithm>
#include <limits.h>
#include <set>
using namespace std;
int main() {
int n;
scanf("%d", &n);
set<int> st;
for (int i = ; i < n; i++) {
int tmp;
scanf("%d", &tmp);
if (tmp > ) {
st.insert(tmp);
}
}
int j = ;
for (auto it = st.begin(); it != st.end(); it++) {
if (*it > j) {
printf("%d", j);
break;
}
else j++;
}
if(j==st.size()+)printf("%d",j);
}
注意点:这里只保证了输入数不超过int大小,用hash不好做,不如直接用set,自动升序排列,找到第一个不满足的数。
第二个坑是测试点2-5都是输入数据连续的,缺失的是输入数据后面那个数
PAT A1144 The Missing Number (20 分)——set的更多相关文章
- 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 A1019 General Palindromic Number (20 分)
AC代码 #include <cstdio> const int max_n = 1000; long long ans[max_n]; int num = 0; void change( ...
- PAT 1144 The Missing Number
1144 The Missing Number (20 分) Given N integers, you are supposed to find the smallest positive in ...
- PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642
PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642 题目描述: A number that will ...
- PAT乙级:1088 三人行 (20分)
PAT乙级:1088 三人行 (20分) 题干 子曰:"三人行,必有我师焉.择其善者而从之,其不善者而改之." 本题给定甲.乙.丙三个人的能力值关系为:甲的能力值确定是 2 位正整 ...
- PAT乙级:1064 朋友数 (20分)
PAT乙级:1064 朋友数 (20分) 题干 如果两个整数各位数字的和是一样的,则被称为是"朋友数",而那个公共的和就是它们的"朋友证号".例如 123 和 ...
- [PAT] 1144 The Missing Number(20 分)
1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...
- PAT 甲级 1035 Password (20 分)
1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...
- PAT 1144 The Missing Number[简单]
1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...
随机推荐
- API接口规范V1.0——制定好规范,才好合作开发
返回码规范: 统一六位 000000 表示成功! 参数相关返回码预留100000-199999:系统相关返回码预留200000-299999:数据中心310000-319999后续项目以此类推,后续根 ...
- js-QuickStart-base.js
// 1.变量(Variables) var myVariable; myVariable = 'Bob'; // 数据类型 string number boolean array object // ...
- spring boot (1):初尝
工具:intellij idea 自定义banner: 控制台console显示的banner 自定义方式,在src/main/resources 下创建banner.txt增加文本即可,生成字符网站 ...
- 【工具相关】Web-Sublime Text2新建立文件夹(二)
紧接着上文. 一,打开Sublime Text2. 二,在桌面上新建立一个文件夹,html5. 三,打开html5如图所示.里面有我们刚刚建立好的文件. 四,把html5文件夹拖动到sublime2中 ...
- REM在edge浏览器中不重新计算解决
经过多分析和排查,此问题解决的方案 第一种: 在CSS样式中添加 body { font-size:100% } 如果不起作用,可以尝试将引用的REMjs放在head内引用
- 《Inside C#》笔记(十四) 反射
通过反射可以在运行时动态地获取一个应用的元数据. 一 反射相关的类和方法 与反射相关的类处在System.Reflection命名空间下,包括Assembly.Module.MethodInfo.Fi ...
- Telephone dialer
运行电话拨号器,需要加这个权限,否则不会打通电话.这个权限是用户权限,是谷歌工程师为了跟自己撇清关系,用户需要权限的时候自己加. 运行android程序的时候提示:ActivityManager: W ...
- python中if else流程判断
_username='Lily' _password=' username=input("username:") password=input("password:&qu ...
- go 实现简单的加权分配
最近一段时间接手了一个golang编写的模块,从python转到golang这种静态语言还是有些不适应的,接手模块后的第一个需求是实现一个加权分配的方法. 简单来说数据库中存有3个链接,3个链接的权重 ...
- postmortem报告
beta阶段与alpha阶段的比较 beta阶段与alpha阶段的比较主要从个人方面和团队方面进行总结. 以下是我们的队员对于自己在beta阶段的实践和alpha阶段的改进的总结. 成员林静雯认为,自 ...