STL next_permutation 和 prev_permutation

如果数组a当前的排序为{1,2,3,5,4}对这5个元素进行排序,找它的下一个排序  next_permutation(a,a+n);找它的前一个就是     prev_permutation(a,a+n);

有了这个函数,对于数组的字典序排列就方便很多啦;

现在看一道例题 HDU 1027

注意头文件名为:

algorithm
每次要清空数组,因为每次数组a的数据都会被改变。
#include<iostream>
#include<cstdio>
#include<string>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main(){
    int n,m;
    int a[1010];
     for(int i=0;i<1002;i++)
        a[i]=i+1;
    while(cin>>n>>m){
      
   
            m--;
   while(m--){
   next_permutation(a,a+n);

   }
    for(int i=0;i<n-1;i++)
     cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
    }

   return 0;
}
原文地址:https://www.cnblogs.com/wintersong/p/5213248.html