next_permutation(start, end)
#include<bits/stdc++.h>
using namespace std;
vector<int> v;
void printV(vector<int> &v){
for(int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << '\\n';
}
int cnt = 0;
int main(){
for(int i = 1; i <= 3; i++) v.push_back(i);
do{
printV(v);
cnt += 1;
}while(next_permutation(v.begin(), v.end()));
cout << cnt << '\\n';
return 0;
}
// 출력
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
6
재귀함수
#include<bits/stdc++.h>
using namespace std;
vector<int> v;
void printV(vector<int> &v){
for(int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << '\\n';
}
void makePermutation(int n, int r, int depth){
if(r == depth){
printV(v);
// logic
return;
}
for(int i = depth; i < n; i++){
swap(v[i], v[depth]);
makePermutation(n, r, depth+1);
swap(v[i], v[depth]);
}
return;
}
int main(){
for(int i = 0; i <= 3; i++)v.push_back(i); makePermutation(3, 3, 0);
return 0;
}