PAT 甲级 1144 The Missing Number

https://pintia.cn/problem-sets/994805342720868352/problems/994805343463260160

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 (≤). 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 <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 10;
int a[maxn], num[maxn];

int main() {
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i ++) {
        scanf("%d", &a[i]);
        if(a[i] < 0 || a[i] > n + 1) continue;
        else
            num[a[i]] ++;
    }

    for(int i = 1; i <= n; i ++) {
        if(num[i] == 0) {
           printf("%d\n", i);
           return 0;
        }
    }
    printf("%d\n", n + 1);
    return 0;
}

  

原文地址:https://www.cnblogs.com/zlrrrr/p/9420588.html

时间: 2024-07-31 07:59:42

PAT 甲级 1144 The Missing Number的相关文章

PAT Advanced 1144 The Missing Number (20分)

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 (≤). Then N integers are giv

1144 The Missing Number (20 分)

1144 The Missing Number (20 分) 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

PAT 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 (≤10^5). Then N integers are

PAT 甲级 1019 General Palindromic Number

https://pintia.cn/problem-sets/994805342720868352/problems/994805487143337984 A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit num

PAT甲级——1104 Sum of Number Segments (数学规律、自动转型)

本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90486252 1104 Sum of Number Segments (20 分) Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence { 0.1, 0.2, 0.3,

PAT甲级——A1019 General Palindromic Number

A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers. Although palindromic numbers are most often cons

PAT 甲级 1019 General Palindromic Number (进制转换,vector运用,一开始2个测试点没过)

1019 General Palindromic Number (20 分)   A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers. Althoug

PAT 甲级 1104 Sum of Number Segments (20分)(有坑,int *int 可能会溢出)

1104 Sum of Number Segments (20分)   Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence { 0.1, 0.2, 0.3, 0.4 }, we have 10 segments: (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0

【PAT甲级】1024 Palindromic Number (25 分)

题意: 输入两个正整数N和K(N<=1e10,k<=100),求K次内N和N的反置相加能否得到一个回文数,输出这个数和最小的操作次数. trick: 1e10的数字相加100次可能达到1e40,所以long long会爆,采用字符数组操作,以及代码注释中遇到的一些小问题. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;char num[107];char s[107];//这里用