生成可重集的排列

来源:互联网 发布:matlab矩阵矢量化 编辑:程序博客网 时间:2024/06/09 17:19

自己重写的下一个排列函数

#include <iostream>#include <algorithm>using namespace std;#define N 100000int p[N];int _next_permutation(int *first, int *last){int *p,*q;for (p = last; p != first; p--){if (*p > *(p - 1)){int *a = p;for (q = p; q != last; q++){if (*q > *(p - 1) && *q < *a){a = q;}}int temp = *a;*a = *(p - 1);*(p - 1) = temp;sort(p, last);return 1;}}return 0;}int main(){int n,i;cin >> n;for (i = 0; i < n; i++)cin >> p[i];sort(p, p + n);do{for (i = 0; i < n; i++)cout << p[i];cout << endl;} while (_next_permutation(p, p + n));return 0;}


0 0