https://school.programmers.co.kr/learn/courses/30/lessons/42839#
#include <string>
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
const bool IsPrime(int n)
{
for (size_t i = 2; i <= sqrt(n); ++i)
if (n % i == 0) return false;
return true && n > 1;
}
void expression(const string& numbers, unordered_set<int>& set, vector<int>* idxList, string num)
{
set.insert(stoi(num));
for (int i = 0; i < numbers.length(); ++i)
{
if (find(idxList->begin(), idxList->end(), i) != idxList->end()) continue;
idxList->push_back(i); num += numbers[i];
expression(numbers, set, idxList, num);
idxList->pop_back(); num.pop_back();
}
}
int solution(string numbers) {
//24.10.21
int answer = 0;
unordered_set<int> set;
expression(numbers, set, new vector<int>(), "0");
for (const auto& n : set)
answer += IsPrime(n) ? 1 : 0;
return answer;
}
'Coding Test > Programmers' 카테고리의 다른 글
[Programmers] (완전탐색) Lv 2. 피로도 (3) | 2024.10.23 |
---|---|
[Programmers] (동적계획법(Dynamic Programming)) Lv 3. 정수 삼각형 (0) | 2024.10.21 |
[Programmers] (정렬) Lv 2. H-Index (0) | 2024.10.17 |
[Programmers] (Summer/Winter Coding(~2018)) Lv 2. 점프와 순간 이동 (0) | 2024.10.17 |
[Programmers] (힙(Heap)) Lv 2. 더 맵게 (0) | 2024.10.15 |
댓글