백준_2023 신기한 소수 (DFS)Algorithm/Algorithm 문제2024. 1. 20. 16:59
Table of Contents
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BJ_2023 {
static int n;
static StringBuilder sb;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
sb = new StringBuilder();
n = Integer.parseInt(br.readLine());
dfs(2,1);
dfs(3,1);
dfs(5,1);
dfs(7,1);
System.out.println(sb);
}
static void dfs(int k, int count) {
if(count == n) {
sb.append(k).append('\n');
return;
}
for(int i=1; i<10; i+=2) {
if(isPrime(k*10 + i)) {
dfs(k*10 + i, count+1);
}
}
}
static boolean isPrime(int k) {
for(int i=2; i<=k/2; i++) {
if(k%i==0) {
return false;
}
}
return true;
}
}
'Algorithm > Algorithm 문제' 카테고리의 다른 글
백준_1260 DFS와BFS (DFS & BFS) (1) | 2024.01.22 |
---|---|
백준_13023 ABCDE (DFS) (0) | 2024.01.21 |
백준_11724 연결요소의 개수 (DFS) (0) | 2024.01.19 |
백준_10989 수 정렬하기3 (기수정렬) - 성공(풀이 참고) (0) | 2024.01.18 |
백준_10989 수 정렬하기 3 (기수 정렬) - 실패(메모리초과) (0) | 2024.01.18 |
@kjyyjk :: 녕의 학습 기록
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!