C++构造 下一个排列 的函数

今天围观刘汝佳神犇的白书发现了一个好用的函数:

next_permutation();

可以用于可重, 或者不可重集, 寻找下一个排列.

时间复杂度尚不明.

//适用于不可重和可重集的排列.

# include <iostream>
# include <algorithm>
using namespace std;

int a[1003], n;

int main()
{
    cin >> n;
    for (int i = 0; i < n; ++i )
        cin >> a[i];
    sort(a, a+n);
    do
    {
        for(int i = 0; i < n; ++i)
            cout << a[i] << ' ';
        cout << endl;

    } while( next_permutation(a, a+n) );
    return 0;
}
posted @ 2014-08-15 16:13  polebug  阅读(540)  评论(0编辑  收藏  举报