View
[λ°±μ€] 2023: μ κΈ°ν μμ (Java)
μ± μ½λ κ°μ 2022. 10. 6. 23:19λ°μν
https://www.acmicpc.net/problem/2023
package λ°±μ€;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//μ κΈ°ν μμ
public class Main_2023 {
static int n;
static int[] pick;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
pick = new int[n];
permutation(0, 0);
}
private static void permutation(int num, int cnt) {// cntλ μ리
if (cnt == n) {
System.out.println(num);
return;
}
for (int i = 1; i <= 9; i++) {
// κ·Έ λμλ
// 2333 μ΄λ©΄ 2 μμ 23 μμ 233μμ 2333μμμ¬μΌνλκ» κ·Έκ±° 체ν¬
if (prime(num * 10 + i)) {// 1μ κ²½μ° 2 3 5 7 μμλ§ μΆλ ₯
permutation(num * 10 + i, cnt + 1); // nμ΄ λλ©΄ κ·Έλ λ§λ€μ΄μ§ μλ₯Ό μΆλ ₯ν¨
}
}
}
private static boolean prime(int num) {
if (num == 1)
return false;
int sqrt = (int) Math.sqrt(num);// μ κ³±κ·Όμ μλ‘ νλ³ -> μκ°μ΄κ³Ό μ€μ΄κΈ° μν¨
// μμμΈμ§ νλ³
for (int i = 2; i <= sqrt; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}
λ°μν
'μκ³ λ¦¬μ¦ > μ½λ©ν μ€νΈ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[νλ‘κ·Έλλ¨Έμ€] 2οΈβ£ 무μΈλ μ¬ν(JAVA) (0) | 2023.02.24 |
---|---|
[λ°±μ€] 14502: μ°κ΅¬μ (Java) (0) | 2022.10.09 |
[SWEA_3307] μ΅μ₯ μ¦κ° μμ΄(LIS, Longest Increasing Subsequence) (0) | 2022.10.06 |
[νλ‘κ·Έλλ¨Έμ€] 보νΈμμμ μ€μ±νν λλ¬Ό (0) | 2022.10.06 |
[λ°±μ€] 2630: μμ’ μ΄ λ§λ€κΈ°(λΆν μ 볡) (0) | 2022.10.03 |
reply