본문 바로가기
Problem Solving/Baekjoon

[백준] 25632 소수 부르기 게임 - Greedy / Java

by graycode 2024. 1. 16.

 문제 링크

 

25632번: 소수 부르기 게임

용태가 부를 수 있는 소수는 $11, 13, 17$이고, 유진이가 부를 수 있는 소수는 $13, 17, 19$이다. 둘 다 최선을 다해서 플레이한다면 $13 → 17 → 11 → 19$로 진행될 수 있다. 용태가 더 이상 부를 소수가

www.acmicpc.net

 

 풀이 코드

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

public class Main {

    static boolean[] nonPrime = new boolean[1001];

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

        sieve();

        Set<Integer> A = new HashSet<>(), B = new HashSet<>();
        int a = read(), b = read(), c = read(), d = read(), cnt = 0;

        for (int i = a; i <= b; i++) if (!nonPrime[i]) A.add(i);

        for (int i = c; i <= d; i++) {
            if (!nonPrime[i]) {
                B.add(i);
                if (A.contains(i)) cnt++;
            }
        }

        bw.write(A.size() == B.size() ? (cnt % 2 == 0 ? "yj" : "yt") : (A.size() < B.size() ? "yj" : "yt"));
        bw.flush();
    }

    private static void sieve() {
        double sqrt = Math.sqrt(nonPrime.length);

        for (int i = 2; i < sqrt; i++) {
            if (nonPrime[i]) continue;
            for (int j = i * i; j < nonPrime.length; j += i) nonPrime[j] = true;
        }
    }

    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;
    }

}

댓글