반응형
소수찾기는 오랜간만인데 잘 진행해서 다행이다.
완탐 & 소수판별을 같이 진행하면 된다.
#include <string>
#include <vector>
#include <set>
using namespace std;
// 1. 완탐 dfs
// 2. 소수판별
bool visit[7];
int len;
int answer;
set<int> s;
bool checkPrime(int number) {
if(number <= 1) return false;
if(number == 2) return true;
for(int i = 2; i * i <= number; i++) {
if(number % i == 0) {
return false;
}
}
return true;
}
void dfs(string numbers, string str) {
if(str.length() != 0) {
s.insert(stoi(str));
}
for(int i = 0; i < len; i++) {
if(!visit[i]) {
visit[i] = true;
dfs(numbers, str + numbers[i]);
visit[i] = false;
}
}
}
int solution(string numbers) {
len = numbers.length();
dfs(numbers, "");
for(auto x : s) {
if(checkPrime(x)) answer++;
}
return answer;
}
반응형
'[알고리즘 + 자료구조] > [프로그래머스]' 카테고리의 다른 글
프로그래머스 더 맵게 C++ (0) | 2023.08.15 |
---|---|
프로그래머스 가장 큰 수 C++ (0) | 2023.08.15 |
프로그래머스 큰 수 만들기 C++ (0) | 2023.08.14 |
프로그래머스 스킬 트리 C++ (0) | 2023.08.14 |
프로그래머스 방문 길이 C++ (0) | 2023.08.12 |