[C/C++] STL bits/stdc++.h 를 사용한 순열

Onam Kwon·2022년 12월 12일
0

C/C++

목록 보기
12/12

순열 STL

  • STL bits/stdc++.h를 이용해 {1,2,3,4}로 구성된 순열을 구하는 방법

🔽main.cpp🔽

#include <bits/stdc++.h>
#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> n = {1,2,3,4};
    vector<vector<int>> v;
    do {
        vector<int> cursor;
        for(const auto &item: n) {
            cursor.push_back(item);
        } 
        v.push_back(cursor);
    } while(next_permutation(n.begin(),n.end()));
    
    for(int i=0;i<v.size();i++) {
        cout<<i<<": ";
        for(const auto &item: v[i]) {
            cout<<item<<" ";
        } cout<<endl;
    }

    return 0;
}

🔽Output🔽

0: 1 2 3 4 
1: 1 2 4 3 
2: 1 3 2 4 
3: 1 3 4 2 
4: 1 4 2 3 
5: 1 4 3 2 
6: 2 1 3 4 
7: 2 1 4 3 
8: 2 3 1 4 
9: 2 3 4 1 
10: 2 4 1 3 
11: 2 4 3 1 
12: 3 1 2 4 
13: 3 1 4 2 
14: 3 2 1 4 
15: 3 2 4 1 
16: 3 4 1 2 
17: 3 4 2 1 
18: 4 1 2 3 
19: 4 1 3 2 
20: 4 2 1 3 
21: 4 2 3 1 
22: 4 3 1 2 
23: 4 3 2 1 
  • do while문과 bits/stdc++.hnext_permutation()을 이용해 구현할 수 있다.
profile
권오남 / Onam Kwon

0개의 댓글