STL next_permutation()

用法

字典序全排列
可以发现函数next_permutation()是按照字典序产生排列的,并且是从数组中当前的字典序开始依次增大直至到最大字典序。

代码

#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
int main(){
    int n;
    int a[10];
    while(cin>>n){
        int i;
        for(i=0;i<n;i++) cin>>a[i];
        cout<<endl;
        //sort(a,a+n);
        do{
            for(i=0;i<n;i++) cout<<a[i]<<' ';
            cout<<endl;
        }while(next_permutation(a,a+n));
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/mch5201314/p/9474975.html