https://www.acmicpc.net/problem/17103
17103번: 골드바흐 파티션
첫째 줄에 테스트 케이스의 개수 T (1 ≤ T ≤ 100)가 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있고, 정수 N은 짝수이고, 2 < N ≤ 1,000,000을 만족한다.
www.acmicpc.net
해석 및 팁
이 문제는 에라토스테네스의 체를 이용해서 풀면 되는 문제입니다. 먼저 1000000까지의 소수를 모두 구해놓은 뒤 범위 내의 소수의 개수를 구하면 됩니다.
Java 코드
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
boolean[] arr= new boolean[1000001];
arr[0] = true;
arr[1] = true;
for(int i = 2; i * i <= 1000000; i++) {
if(arr[i] == true) continue;
for(int j = i * i; j <= 1000000; j += i) arr[j] = true;
}
int t = sc.nextInt();
for(int i = 0; i < t; i++) {
int count = 0;
int n = sc.nextInt();
for(int j = 2; j <= n; j++) {
if(arr[j] == false && arr[n-j] == false) count++;
}
if(count % 2 == 1) sb.append(count / 2 + 1).append("\n");
else sb.append(count / 2).append("\n");
}
System.out.println(sb);
}
}
'백준' 카테고리의 다른 글
백준 6588 골드바흐의 추측(Java) (0) | 2023.02.12 |
---|---|
백준 15965 K번째 소수(Java) (0) | 2023.02.12 |
백준 2178 미로 탐색(Java) (0) | 2023.02.12 |
백준 9658 돌 게임4(Java) (0) | 2023.02.12 |
백준 11501 주식(Java) (0) | 2023.02.12 |