본문 바로가기

problem solving

[프로그래머스]소수 찾기

#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
bool check[8];
vector<string> store;
string str;
 
//부분수열의 합
void DFS(string val) {
    store.push_back(val);
 
    for(int idx = 0; idx < str.length(); idx++) {
        if(!check[idx]) {
            check[idx] = true;
            DFS(val + str[idx]);
            check[idx] = false;
        }
    }
 
}
 
bool checkDecimal[10000001];
bool decimal[10000001];
 
int solution(string numbers) {
    //부분수열의 합
    str = numbers;
    fill(check, check + 80);
    DFS("");
    //처음값인 ""제거
    store.erase(store.begin());
 
    //소수
    fill(decimal, decimal + 10000001false);
    fill(checkDecimal, checkDecimal + 10000001false);
    for(int idx = 2; idx < 10000001; idx++) {
        if(checkDecimal[idx]) { continue; }
        decimal[idx] = true;
        int count = 1;
        while(idx * count < 10000001) {
            checkDecimal[idx * count] = true;
            count++;
        }
    }
 
    //같은 값을 가려내기위해 정렬
    sort(store.begin(), store.end());
    store.push_back("0");
 
    int answer = 0;
    for(int idx = 0; idx < store.size() - 1; idx++) {
        if(store[idx] == store[idx + 1|| store[idx][0== '0') { continue; }
        if(!decimal[stoi(store[idx])]) { continue; }
        answer++;
    }
 
    return answer;
}
cs

'problem solving' 카테고리의 다른 글

[프로그래머스]H-index  (0) 2020.03.10
[프로그래머스]더 맵게  (0) 2020.03.10
[프로그래머스]큰 수 만들기  (0) 2020.03.09
[프로그래머스]가장 큰 수  (0) 2020.03.09
[프로그래머스]조이스틱  (0) 2020.03.08