728x90
순열
#include <vector>
#include <iostream>
using namespace std;
int arr[5] = {1,2,3,4,5};
bool check[5];
vector<int> number;
int n,m; //nPm
void dfs(int depth)
{
if(depth == m)
{
for(int i=0;i<number.size();i++)
{
cout << number[i] << ' ';
}
cout << endl;
return;
}
for(int i=0;i<n;i++)
{
if(check[i])continue;
check[i] = true;
number.push_back(arr[i]);
dfs(depth+1);
number.pop_back();
check[i] = false;
}
}
조합
#include <iostream>
#include <vector>
using namespace std;
int arr[5] = {1,2,3,4,5};
bool check[5];
int n,m;//nCm
void dfs(int depth, int idx)
{
if(depth == m)
{
for(int i=0;i<n;i++)
{
if(check[i])
{
cout << arr[i]<< ' ';
}
}
cout << endl;
return;
}
for(int i=idx;i<n;i++)
{
if(check[i])continue;
check[i] = true;
dfs(depth+1, i);
check[i] = false;
}
}
중복순열
#include <iostream>
using namespace std;
int arr[5] = {1,2,3,4,5};
int arr2[5];
int n,m;
void dfs(int depth)
{
if(depth == m)
{
for(int i=0;i<m;i++)
{
cout << arr2[i]<< ' ';
}
cout << endl;
return;
}
for(int i=0;i<n;i++)
{
arr2[depth] = arr[i];
dfs(depth+1);
}
}
중복조합
#include <iostream>
#include <vector>
using namespace std;
int n,m;
int arr[5] = {1,2,3,4,5};
vector<int> v(5,0);
void dfs(int idx, int depth)
{
if(depth == m)
{
for(int i=0;i<m;i++)
{
cout << v[i] << ' ';
}
cout << endl;
return;
}
for(int i=idx;i<n;i++)
{
v[depth] = arr[i];
dfs(i, depth+1);
}
}
LIST
'Programming > Algorithm' 카테고리의 다른 글
[Level2 프로그래머스] 더 맵게 c++ (0) | 2021.09.24 |
---|---|
[Level2 프로그래머스] 기능개발 c++ (0) | 2021.09.24 |
[Level2 프로그래머스] 단체사진 찍기 c++ (2) | 2021.09.24 |
[Level2 프로그래머스] 카카오프렌즈 컬러링북 (0) | 2021.09.24 |
[Level2 프로그래머스] 오픈채팅방 c++ (0) | 2021.09.24 |