일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- 싸이월드
- 스탠실 버퍼 사용
- 스탠실 버퍼 시작
- Expo
- react
- react native typescript
- GitHub
- react-native
- html
- react native accessible
- C++
- node
- react native mac
- cyworld
- stencil buffer
- 리액트 네이티브 맥
- react native typescript navigate
- react native 타입스크립트
- 스탠실 버퍼 튜토리얼
- react native typescript navigation
- node.js
- react native
- 리액트 네이티브 설치 오류
- 벡터와 리스트의 차이
- javascript
- unity stencil buffer
- react native ios 기기 연결
- c++ 정보은닉
- c++ using
- CSS
Archives
- Today
- Total
혀니의 이거저거 뿌시기
[C++]프로그래머스 소수 찾기 (next_permutation) 본문
728x90
코딩테스트 연습 - 소수 찾기 | 프로그래머스 스쿨 (programmers.co.kr)
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
bool isPrime(int n){ //외워두는 게 좋을 것
if(n < 2) return false;
for(int i = 2; i * i <= n; i++)
if (n % i == 0) return false;
return true;
}
int solution(string numbers) {
int answer = 0;
vector<char> v;
vector<int> nums;
for(int i = 0; i < numbers.size(); i++)
v.push_back(numbers[i]);//한글자씩 vector에 저장
sort(v.begin(), v.end());//오름차순 정렬
do{
string temp= "";
for(int i = 0; i <v.size(); i++)//모든 순열마다 문자열 길이 하나씩 게 해서 배열에 집어넣음
{
temp.push_back(v[i]); //if 123이면 temp= 1 12 123
nums.push_back(stoi(temp));//if 123이면 nums 1 12 123
}
}while(next_permutation(v.begin(), v.end()));//모든순열(next_permutation이용해 do,while사용)
sort(nums.begin(), nums.end());//완성된 nums를 오름차순 정렬(크기다른 모든 순열이 들어있음)
nums.erase(unique(nums.begin(), nums.end()), nums.end()); (nums.erase(unique(), nums.end())를 사용해 중복 제거)
for(int i=0; i< nums.size(); i++)
if(isPrime(nums[i]))//중복이 제거된 모든 순열 nums에서 isPrime함수를 이용해 answer를 구한다.
answer++;
return answer;
}
nums.erase(unique(nums.begin(), nums.end()), nums.end()); //외워두기
do{
}while(next_permutation(v.begin(), v.end())); //외워두기2
bool isPrime(int n)
{
if(n < 2)
return false;
for(int i = 2; i*i <= n; i++)
{
if(n % 2 == 0)
return false;
}
return true;
} //외워두기 3
728x90
'알고리즘 공부(C++)' 카테고리의 다른 글
BFS 이해 (0) | 2023.06.11 |
---|---|
[C++]프로그래머스 네트워크 (0) | 2023.06.08 |
[C++]프로그래머스 숫자 변환하기(BFS) (0) | 2023.06.04 |
프로그래머스 C++ 타겟 넘버 (DFS) (0) | 2023.05.30 |
백준 12782 비트 우정지수 (파이썬/그리디) (1) | 2022.10.08 |