반응형

https://www.acmicpc.net/problem/2023

 

2023번: 신기한 소수

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

www.acmicpc.net

 

한 자리수의 뒤에 N 자리수까지 숫자를 덧붙여나가며, 덧붙여진 수가 소수인지 검사해주었습니다.

 

#include <iostream>
#include <cmath>
using namespace std;

int n;

bool isPrime(int num) {
	if (num < 2) return false;
	int mx = sqrt(num);
	for (int i = 2; i <= mx; i++) {
		if (num % i == 0) return false;
	}
	return true;
}

void dfs(int num, int depth) {
	if (depth == n) {
		cout << num << "\n";
		return;
	}

	for (int i = 1; i < 10; i++) {
		int next = num * 10 + i;
		if (isPrime(next))
			dfs(next, depth + 1);
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	cin >> n;
	dfs(0, 0);
}
반응형

'Algorithm' 카테고리의 다른 글

백준 17404 : RGB거리 2  (0) 2021.11.19
백준 2836 : 수상 택시  (0) 2021.11.19
백준 4181 : Convex Hull  (0) 2021.11.18
백준 2699 : 격자점 컨벡스헐  (0) 2021.11.18
백준 1310 : 달리기 코스  (0) 2021.11.18

+ Recent posts