본문 바로가기
Problem Solving/Baekjoon

[백준] 1773 폭죽쇼 - Brute Force / Java

by graycode 2023. 12. 26.

 문제 링크

 

1773번: 폭죽쇼

2 1 2 1 1 1 2 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 위의 그림에서 1,2가 쓰여있는 4, 6, 8, 12, 16, 18, 20초에 폭죽이 밤 하늘에 터진다. 단 12초에는 두 폭죽이 동시에 하늘에 터지지만 한

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Main {

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

        int n = read(), c = read(), p;
        boolean[] arr = new boolean[c + 1];

        while (n-- > 0) for (int i = p = read(); i <= c; i += p) arr[i] = true;

        int cnt = 0;
        for (boolean b : arr) if (b) 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;
    }

}

댓글