본문 바로가기
Problem Solving/Baekjoon

[백준] 24081 箱と鍵 (Boxes and Keys) - Brute Force / Java

by graycode 2025. 4. 19.

 문제 링크

https://www.acmicpc.net/problem/24081

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int[] arr = new int[read()];
        int m = read();
        for (int i = 0; i < arr.length; i++) arr[i] = read();

        Set<Integer> set = new HashSet<>();
        while (m-- > 0) set.add(read());

        int cnt = 0;
        for (int i : arr) if (set.contains(i)) cnt++;

        bw.write(String.valueOf(cnt));
        bw.flush();
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글