- 소수판별 알고리즘
- set 활용법 : iterator, insert
- string에도 begin(), end()
- next_permutation : 순열정렬 라이브러리 함수 *적용전 배열 sort 필요
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;
bool isPrime(int a){
if(a == 0 || a == 1)
return false;
if(a == 2)
return true;
if(a % 2 == 0)
return false;
for(int i=2; i<=sqrt(a); i++){
if(a%i == 0)
return false;
}
return true;
}
int solution(string numbers) {
int answer = 0;
string num = numbers;
sort(num.begin(), num.end());
int nSize = num.length();
set<int> sn;
int tmp;
do{
for(int i=1; i<=nSize; i++){
tmp = stoi(num.substr(0, i));
sn.insert(tmp);
}
}while(next_permutation(num.begin(), num.end()));
for(set<int>::iterator it = sn.begin(); it != sn.end(); it++){
if(isPrime(*it))
answer++;
}
return answer;
}
'알고리즘 > 알고리즘' 카테고리의 다른 글
[C++] 백준 5582번 : 공통 부분 문자열 (0) | 2022.05.08 |
---|---|
[C++] 백준 20413번 : MVP 다이아몬드 (Easy) (0) | 2022.05.08 |
[C++] 프로그래머스 : 코딩테스트연습_정렬 (0) | 2022.02.21 |
[C++] 프로그래머스 : 코딩테스트연습_힙 (0) | 2022.02.15 |
[C++] 프로그래머스 : 코딩테스트연습_스택/큐 (0) | 2022.02.10 |