๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Programmers)

[c++] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ์†Œ์ˆ˜ ์ฐพ๊ธฐ(Level 2)

by dkswnkk 2022. 4. 13.

๋ฌธ์ œ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์†Œ์ˆ˜ ์ฐพ๊ธฐ

ํ•œ์ž๋ฆฌ ์ˆซ์ž๊ฐ€ ์ ํžŒ ์ข…์ด ์กฐ๊ฐ์ด ํฉ์–ด์ ธ์žˆ์Šต๋‹ˆ๋‹ค. ํฉ์–ด์ง„ ์ข…์ด ์กฐ๊ฐ์„ ๋ถ™์—ฌ ์†Œ์ˆ˜๋ฅผ ๋ช‡ ๊ฐœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋‚ด๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์ข…์ด ์กฐ๊ฐ์— ์ ํžŒ ์ˆซ์ž๊ฐ€ ์ ํžŒ ๋ฌธ์ž์—ด numbers๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ข…์ด

programmers.co.kr

 

์ฝ”๋“œ

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

#define MAX 9999999+1
using namespace std;
int visited[MAX];
vector<bool> prime (MAX, true); // true์ผ๋•Œ ์†Œ์ˆ˜

int cnt = 0;

void permutation(string inp, string origin, int index){
    if(inp.length()>origin.length()) return;
    
    string number = inp;
    reverse(number.begin(),number.end());
    while(!number.empty()&&number.back()==0) number.pop_back(); // 0 ์ œ๊ฑฐ
    reverse(number.begin(),number.end());
    if(!number.empty()&&prime[stoi(number)]&&!visited[stoi(number)]){   //์†Œ์ˆ˜๋ผ๋ฉด
        visited[stoi(number)] = 1;  //์ค‘๋ณต ๋ฐฉ์ง€๋ฅผ ์œ„ํ•œ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
        //        cout<<"์กฐํ•ฉ: "<< stoi(number)<<'\n';
        cnt++;
    }
    
    
    for(int i=index; i<origin.length(); i++){
        string temp = number + origin[i];
        permutation(temp, origin, i+1);
    }
}
int solution(string numbers) {
    int answer = 0;
    
    prime[0] = false, prime[1] = false;
    for(int i=2; i<=MAX; i++){  //์†Œ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด
        if (prime[i] == true) {
            int k = 2;
            while (i * k <= MAX) {
                prime[i * k] = false;
                k++;
            }
        }
    }
    sort(numbers.begin(),numbers.end());   //์ˆœ์—ด ๋Œ๋ฆฌ๊ธฐ ์ „์—๋Š” ํ•„์ˆ˜๋กœ ์ •๋ ฌํ•ด์•ผํ•จ
    do{ //์ˆœ์—ด
        permutation("",numbers, 0);
    }while(next_permutation(numbers.begin(),numbers.end()));
    
    answer = cnt;
    return answer;
}

 

ํ’€์ด(31๋ถ„)

๋จผ์ € ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด์„œ ์†Œ์ˆ˜(prime number)๋ฅผ ๋ฏธ๋ฆฌ ์ „๋ถ€ ๊ตฌํ•ด์ค๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์„ ์ˆœ์—ด๋กœ ๋Œ๋ ค์„œ ์กฐํ•ฉํ–ˆ์„ ๋•Œ ๊ทธ ์ˆ˜๊ฐ€ ์†Œ์ˆ˜์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ์™„์ „ ํƒ์ƒ‰ ๋ฌธ์ œ์˜€๋Š”๋ฐ, ์ง€๊ธˆ ์ฒ˜์Œ์— next_permutation์„ ์ง„ํ–‰ํ•˜๊ธฐ ์ „์— ์ •๋ ฌ์„ ํ•ด์ค˜์•ผ ํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ๊นœ๋นกํ•˜๊ณ  ์ œ์ถœํ–ˆ๋‹ค๊ฐ€ 75์ ์ด ๋– ์„œ ์ด์ƒํ•œ ๋ฐ์„œ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ์‹œ๊ฐ„์„ ์กฐ๊ธˆ ๋งŽ์ด ์†Œ๋น„ํ–ˆ์Šต๋‹ˆ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜๋Š” ์žฌ๊ท€ ๋ง๊ณ  for๋ฌธ์„ ์ด์šฉํ•œ ํ’€์ด์ž…๋‹ˆ๋‹ค. ์†๋„๋Š” ์•„๋ž˜๊ฐ€ ์กฐ๊ธˆ ๋” ๋Š๋ ธ์Šต๋‹ˆ๋‹ค.

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

#define MAX 9999999+1
using namespace std;
int visited[MAX];
vector<bool> prime (MAX, true); // true์ผ๋•Œ ์†Œ์ˆ˜

int cnt = 0;

void permutation(string number){
    string temp;
    for(int i=0; i<number.length(); i++){
        if(temp.empty()&&number[i]==0) continue;
        temp+=number[i];
        if(prime[stoi(temp)]&&!visited[stoi(temp)]){
            visited[stoi(temp)] = 1;
            cnt++;
        }
    }
}

int solution(string numbers) {
    int answer = 0;
    prime[0] = false; prime[1] = false;
    for(int i=2; i<=MAX; i++){  //์†Œ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด
        if (prime[i] == true) {
            int k = 2;
            while (i * k <= MAX) {
                prime[i * k] = false;
                k++;
            }
        }
    }
    sort(numbers.begin(),numbers.end());   //์ˆœ์—ด ๋Œ๋ฆฌ๊ธฐ ์ „์—๋Š” ํ•„์ˆ˜๋กœ ์ •๋ ฌํ•ด์•ผํ•จ
    do{ //์ˆœ์—ด
        permutation(numbers);
    }while(next_permutation(numbers.begin(),numbers.end()));
    
    answer = cnt;
    return answer;
}

๋Œ“๊ธ€