• 문제 링크
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;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 11867 박스 나누기 게임 - Dynamic Programming / Java (0) | 2024.01.18 |
---|---|
[백준] 4811 알약 - Dynamic Programming / Java (0) | 2024.01.17 |
[백준] 31066 비 오는 날 - Greedy / Java (0) | 2024.01.15 |
[백준] 25287 순열 정렬 - Greedy / Java (0) | 2024.01.14 |
[백준] 2864 5와 6의 차이 - Greedy / Java (0) | 2024.01.13 |
댓글