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

猜你喜欢

转载自www.cnblogs.com/younanyihu/p/12366943.html