⬆︎
×

[PAT-A] 1144 The Missing Number

Hyplus目录

Java

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());

        Set<Integer> s = new HashSet<>();
        StringTokenizer st = new StringTokenizer(br.readLine());

        for (int i = 0; i < n; i++) {
            int x = Integer.parseInt(st.nextToken());
            if (x > 0) {
                s.add(x);
            }
        }

        for (int x = 1; ; x++) {
            if (!s.contains(x)) {
                System.out.print(x);
                break;
            }
        }
    }
}

C++

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_set>

using namespace std;

const int N = 10010;

int n;
unordered_set<int> s;

int main() {
    scanf("%d", &n);
    while (n--) {
        int x;
        scanf("%d", &x);
        if (x > 0) s.insert(x);
    }

    for (int x = 1;; ++x)
        if (!s.count(x)) {
            printf("%d", x);
            break;
        }

    return 0;
};

发表评论