Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 |
Tags
- 프로그래머스 #python #2021카카오 #카카오코테 #카카오인턴쉽
- 백준 #백준알고리즘 #알고리즘 #코딩테스트 #코딩테스트준비 #코테준비 #백준2110 #python #문제풀이
- 동
- 백준 #백준2217 #백준로프 #python
- 카카오 코테
- 그리디알고리즘 #그리디 #백준 #우선순위큐 #최소힙 #최대힙 #알고리즘 #코딩테스트 #python
- 프로그래머스 #c++ #코딩테스트
- 백준 #이거다시풀기
- 프로그래머스 #NULL 처리하기
- 프로그래머스 #네트워크 #c++ #코딩테스트 #코테 #코테준비 #dfs
- 프로그래머스 #python #코딩테스트 #코테공부 #알고리즘 #dict
- 프로그래머스 #sql #mysql #코딩테스트
- 카카오 #프로그래머스 #python #코딩테스트 #오픈채팅방
Archives
- Today
- Total
say repository
[프로그래머스] 소수 찾기 c++ 본문
728x90
https://programmers.co.kr/learn/courses/30/lessons/42839
출처:프로그래머스
코딩테스트 연습 - 소수 찾기
한자리 숫자가 적힌 종이 조각이 흩어져있습니다. 흩어진 종이 조각을 붙여 소수를 몇 개 만들 수 있는지 알아내려 합니다. 각 종이 조각에 적힌 숫자가 적힌 문자열 numbers가 주어졌을 때, 종이
programmers.co.kr
#include <string>
#include <vector>
#include <cmath>
#include <set>
#include <iostream>
using namespace std;
set<int> numberSet;
//에라토스테네스의 체
bool isPrime(int n){
if (n<2)
return false;
for (int i=2; i<=sqrt(n); i++){
if (n%i == 0)
return false;
}
return true;
}
//numbers로 숫자조합 만들기 -> numberSet 셋에 삽입
void makeCombination(string comb, string others){
if (comb != "")
numberSet.insert(stoi(comb)); //string to int
for (int i=0; i<others.size(); i++)
//재귀함수
makeCombination(comb + others[i], others.substr(0,i) + others.substr(i+1));
}
int solution(string numbers) {
int answer = 0;
//numbers로 가능한 숫자 조합 만들기
makeCombination("", numbers);
set<int>::iterator iter;
for(iter = numberSet.begin(); iter!=numberSet.end(); iter++){
if(isPrime(*iter))
answer++;
}
return answer;
}
python으로 준비하다가 c++하니까 너무 헷갈린다..ㅋㅋㅋㅋㅋ ㅠㅠ
'알고리즘 문제 풀이 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 타겟 넘버 c++ (0) | 2022.03.18 |
---|---|
[프로그래머스] 카펫 c++ (0) | 2022.03.17 |
[프로그래머스] 모의고사 c++ (0) | 2022.03.17 |
[프로그래머스] 우유와 요거트가 담긴 장바구니 mysql (*) (0) | 2022.02.11 |
[프로그래머스] 헤비 유저가 소유한 장소 MYSQL (*) (0) | 2022.02.11 |