next_permutation()的使用方法

如n==3时的全排列为

123

132

213

231

312

321

代码如下:

#include<iostream>

#include<algorithm>

using namespace std;

int main(){

int a[10] = { 1,2,3};

//a[0]~a[2]之间的序列需要求解next_permutation

do{

cout<<a[0]<<a[1]<<a[2]<<endl;

}while(next_permutation(a,a+3));        //当前为最后一个全排列时返回为0

//此时若再调用next_permutation,a[0]~a[2]将变回初试模样

}

 

输出结果为:

123

132

213

231

312

321

posted @ 2020-02-26 14:37  易难  阅读(149)  评论(0)    收藏  举报