Skip to content

Commit 9b31649

Browse files
authored
Merge pull request #1697 from AlgorithmWithGod/ksinji
[20251217] BOJ / G5 / 신기한 소수 / 강신지
2 parents cfcda34 + e9c5121 commit 9b31649

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static int N;
7+
static StringBuilder sb = new StringBuilder();
8+
9+
public static void main(String[] args) throws Exception {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
N = Integer.parseInt(br.readLine());
12+
13+
int[] start = {2, 3, 5, 7};
14+
for (int s : start) dfs(s, 1);
15+
16+
System.out.print(sb);
17+
}
18+
19+
static boolean isPrime(int x) {
20+
if (x < 2) return false;
21+
if (x == 2) return true;
22+
if (x % 2 == 0) return false;
23+
for (int i = 3; i * i <= x; i += 2) {
24+
if (x % i == 0) return false;
25+
}
26+
return true;
27+
}
28+
29+
static void dfs(int num, int depth) {
30+
if (depth == N) {
31+
sb.append(num).append('\n');
32+
return;
33+
}
34+
for (int d = 1; d <= 9; d++) {
35+
int next = num * 10 + d;
36+
if (isPrime(next)) dfs(next, depth + 1);
37+
}
38+
}
39+
}
40+
```

0 commit comments

Comments
 (0)