PAT_B_1065_Java(17分)_C++(25分)

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

public class Main {
    public static void main(String[] args) throws IOException {

        //输入
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        int n = Integer.parseInt(bf.readLine());
        String[][] str = new String[n][2];
        for (int i = 0; i < n; i++) {
            str[i] = bf.readLine().split(" ");
        }
        int num = Integer.parseInt(bf.readLine());
        String[] buf = bf.readLine().split(" ");

        //
        Set<String> set = new TreeSet<>();
        StringBuffer sb=new StringBuffer("");
        for (int i = 0; i < num; i++) {
            set.add(buf[i]);
        }

        for (int j=0;j<n;j++){
            if(set.contains(str[j][0])&&set.contains(str[j][1])){
                num-=2;
                set.remove(str[j][0]);
                set.remove(str[j][1]);
            }
            else {
                continue;
            }
        }
        System.out.println(num);
        boolean isFirst = true;
        for (String id : set) {
            if (!isFirst) {
                System.out.print(" ");
            }
            System.out.print(id);
            isFirst = false;
        }
        out.flush();
    }
}
[](https://www.liuchuo.net/archives/2740)
#include <iostream>
#include <vector>
#include <set>
using namespace std;
int main() {
    int n, a, b, m;
    scanf("%d", &n);
    vector<int> couple(100000, -1);
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &a, &b);
        couple[a] = b;
        couple[b] = a;
    }
    scanf("%d", &m);
    vector<int> guest(m), isExist(100000);
    for (int i = 0; i < m; i++) {
        scanf("%d", &guest[i]);
        if (couple[guest[i]] != -1)
            isExist[couple[guest[i]]] = 1;
    }
    set<int> s;
    for (int i = 0; i < m; i++) {
        if (!isExist[guest[i]])
            s.insert(guest[i]);
    }
    printf("%d\n", s.size());
    for (auto it = s.begin(); it != s.end(); it++) {
        if (it != s.begin()) printf(" ");
        printf("%05d", *it);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43511405/article/details/107423922