본문 바로가기
Problem Solving/Baekjoon

[백준] 10104 Party Invitation - Data Structure / Java

by graycode 2023. 12. 11.

 문제 링크

 

10104번: Party Invitation

The first line of input contains the integer K (1 ≤ K ≤ 100). The second line of input contains the integer m (1 ≤ m ≤ 10), which is the number of rounds of removal. The next m lines each contain one integer. The ith of these lines (1 ≤ i ≤ m)

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class Main {

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

        int k = read();
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i <= k; i++) list.add(i);

        int m = read();
        while (m-- > 0) {
            int r = read();
            for (int i = 1; i <= list.size(); i++) if (i % r == 0) list.set(i - 1, 0);

            Predicate<Integer> filter = new Predicate<Integer>() {
                @Override
                public boolean test(Integer i) {
                    return i.equals(0);
                }
            };

            list.removeIf(filter);
        }

        for (int i : list) sb.append(i).append("\n");

        bw.write(sb.toString());
        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;
    }

}

댓글