본문 바로가기
TIL/Java 알고리즘 문제 풀이

23.07.07

by J1-H00N 2023. 7. 7.

이번에는 백준에서 유형별로 풀어보자 해서 백준을 풀어보았다.

 

코드는 완성을 했으나 무엇이 문제인지 계속해서 런타임 에러가 발생했다.

백준은 어디가 문제인지, 따로 규격도 정해지지 않아서 더욱 어려웠다.

public class Main {
    public static void main(String[] args) throws Exception {
        new Main().solution();
        exit(0);
    }

    public void solution() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        int N = Integer.parseInt(br.readLine());
        if (N > 100000 || N < 1) {
            throw new RuntimeException();
        }
        int[] A = new int[N];
        for (int i = 0; i < N; i++) {
            A[i] = Integer.parseInt(br.readLine());
        }
        int M = Integer.parseInt(br.readLine());
        if (M > 100000 || M < 1) {
            throw new RuntimeException();
        }
        int[] B = new int[M];
        for (int i = 0; i < M; i++) {
            B[i] = Integer.parseInt(br.readLine());
        }
        int[] answer = new int[M];
        for (int i = 0; i < M; i++) {
            boolean match = false;
            int b = B[i];
            for (int j = 0; j < N; j++) {
                int a = A[j];
                if (a == b) {
                    match = true;
                    break;
                }
            }
            if (match) {
                answer[i] = 1;
            } else {
                answer[i] = 0;
            }
        }
        String data = Arrays.toString(answer);
        sb.append(data);
        System.out.println(sb);
    }
}

exit(0), StringBuilder도 써보았으나 문제 해결은 되지 않았다.

 

 

'TIL > Java 알고리즘 문제 풀이' 카테고리의 다른 글

23.07.30  (0) 2023.07.30
23.07.01  (0) 2023.07.01
23.06.29  (0) 2023.06.29
23.06.24  (0) 2023.06.24
23.06.23  (0) 2023.06.23