다음 순열

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    vector<int> v;
    
    for(int i=0;i<5;i++)
        v.push_back(i);
    
    sort(v.begin(), v.end());
    do{
        for(auto x:v)
            cout<<x<<' ';
        cout<<'\n';
    }while(next_permutation(v.begin(), v.end()));

    return 0;
}

다음 순열을 구할 수 있는 함수로 next_permutation()메서드를 활용할 수 있다. 매개 변수로는 시작 주소 그리고 마지막 주소를 넘겨주면 되겠다. 유의해야 할 사항으로는 sort()로 먼저 정렬을 해줘야 올바른 결과를 도출할 수 있다는 점이다.

좋은 웹페이지 즐겨찾기