next-permutation与prev-permutation及递归实现全排列
2017-02-09 17:14
337 查看
记录下顺序全排列与逆序全排列的stl函数运用方法,只记录用法,不再写俩函数实现具体过程,毕竟我不会QAQ,注:第二种方法默认从小到大序列
#include<iostream> #include<algorithm> using namespace std; int cmp(int a,int b) { return a>b?1:0; } int main(void) { int n,p[100],cur=0; cin>>n; for(int i=0;i<n;++i) cin>>p[i]; sort(p,p+n,cmp); do{ //prev_permutation()先进行从大到小的排序 for(int i=0;i<n;i++) cout<<p[i]<<" "; cout<<endl; } while(prev_permutation(p,p+n)); //next_permutation()需要对数组先进行从小到大排序才能打印出全排列 /*do{ for(int i=0;i<n;i++) cout<<p[i]<<" "; cout<<endl; } while(next_permutation(p,p+n)); */ return 0; }
#include<iostream> #include<algorithm> using namespace std; int n,a[1024],b[1024]; void creat(int m) { int i, j; if (m == n) { for (i = 0; i < n; i++) if (b[i])cout << b[i]<<" "; cout << endl; return; } else { for (i = 0; i < n; i++) if(!i||a[i]!=a[i-1]){ int x = 0, y = 0; for (j = 0; j < m; j++) if (b[j] == a[i])x++; for (j = 0; j < n; j++) if (a[j] == a[i])y++; if (x < y) { b[m] = a[i]; creat(m + 1); } } } } int main(void) { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; memset(b, 0, sizeof(b)); creat(0); system("pause"); return 0; }
相关文章推荐
- next_permutation 与 prev_permutation的用法 (全排列)
- 全排列,next-permutation 简单实现
- STL算法之 next_permutation、prev_permutation 的原理和实现
- next_permutation和prev_permutation(全排列)
- 递归回溯 求N个数的全排列 及 next_permutation的使用
- 全排列(next_permutation,递归)
- stl库之next_permutation实现全排列(二)
- STL实现全排列 next_permutation
- STL实现全排列 next_permutation
- poj3187(next_permutation实现全排列+杨辉三角)
- 全排列(一)next_permutation方式实现
- next_permutation()函数 (数字1~10内实现全排列)
- 九度OJ 1120 全排列 -- 实现C++STL中next_permutation()
- 全排列的递归和非递归实现(permutation)(C++)
- C++实现全排列(调用next_permutation)
- 九度OJ 1120 全排列 -- 实现C++STL中next_permutation()
- 用STL的next_permutation算法实现全排列
- {递归}实现的next_permutation
- 算法——全排列递归实现
- C++全排列递归实现