녕의 학습 기록

백준_2023 신기한 소수 (DFS) 본문

Algorithm/Algorithm 문제

백준_2023 신기한 소수 (DFS)

kjyyjk 2024. 1. 20. 16:59

 

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;
    }
}

 

 

2023번: 신기한 소수

수빈이가 세상에서 가장 좋아하는 것은 소수이고, 취미는 소수를 가지고 노는 것이다. 요즘 수빈이가 가장 관심있어 하는 소수는 7331이다. 7331은 소수인데, 신기하게도 733도 소수이고, 73도 소수

www.acmicpc.net