티스토리 뷰
문제 출처 - https://programmers.co.kr/learn/courses/30/lessons/42839
코딩테스트 연습 - 소수 찾기 | 프로그래머스
한자리 숫자가 적힌 종이 조각이 흩어져있습니다. 흩어진 종이 조각을 붙여 소수를 몇 개 만들 수 있는지 알아내려 합니다. 각 종이 조각에 적힌 숫자가 적힌 문자열 numbers가 주어졌을 때, 종이 조각으로 만들 수 있는 소수가 몇 개인지 return 하도록 solution 함수를 완성해주세요. 제한사항 numbers는 길이 1 이상 7 이하인 문자열입니다. numbers는 0~9까지 숫자만으로 이루어져 있습니다. 013은 0, 1, 3 숫자가 적힌 종이
programmers.co.kr
/* 순열을 이용해서 품
* numbers의 모든 조합을 찾고 그중 소수인 것을 판별
* set을 이용해 중복 제거
*/
#include <string>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
bool isPrime(int num) {
if(num == 1) return false;
if(num == 2) return true;
for(int i=2; i<=sqrt(num); i++) {
if(num % i == 0) return false;
}
return true;
}
int solution(string numbers) {
int answer = 0;
string s = numbers;
sort(s.begin(), s.end(), greater<char>());
vector<bool> prime(stoi(s) + 1);
prime[0] = false;
for(long long i=1; i<prime.size(); i++) {
prime[i] = isPrime(i);
}
sort(numbers.begin(), numbers.end());
set<int> ans;
do{
for(int i=1; i<=numbers.size(); i++) {
string tmp = numbers.substr(0, i);
if(prime[stoi(tmp)]) {
ans.insert(stoi(tmp));
}
}
} while(next_permutation(numbers.begin(), numbers.end()));
return ans.size();;
}
'ALGORITHM > 프로그래머스' 카테고리의 다른 글
[Python]프로그래머스 - 가운데 글자 가져오기(level1) (0) | 2020.02.08 |
---|---|
[Python]프로그래머스 - 모의고사(level1) (0) | 2020.02.07 |
[C++]프로그래머스 - 같은 숫자는 싫어(level1) (0) | 2019.12.27 |
[C++]프로그래머스 - 단어 변환(level3) (0) | 2019.12.06 |
[C++]프로그래머스 - 124 나라의 숫자(level2) (0) | 2019.12.06 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 백준
- 문자열처리
- 정렬
- 스택
- 딕셔너리
- 괄호
- 프로그래머스
- 순열
- 힙
- dictionary
- 재귀
- SWExpert
- 완전탐색
- 문자열
- left join
- programmers
- 파이썬
- SW Expert
- Python
- BOJ
- 우선순위큐
- 2019 Kakao Blind Recruitment
- combination
- Permutation
- C++
- hash
- 코딩테스트
- 구현
- 2020 KAKAO BLIND RECRUITMENT
- 해시
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함