排列模板0.0

来源:互联网 发布:敏捷软件开发流程 编辑:程序博客网 时间:2024/06/05 03:56
#include<iostream>#include<cstring>using namespace std;const int maxn = 100;int Num[maxn];int tmp[maxn];void Printf(int n,int *A,int cur) {if(cur == n) {for(int i = 0; i < n; ++i) cout << A[i];cout << endl;}else {  for(int i = 1; i <= n; ++i) {  bool jug = true;  for(int j = 0; j < cur; ++j)   if(A[j] == i) jug = false;  if(jug) {  A[cur] = i;  Printf(n,A,cur+1);  }}  }}void Printf2(int n,int *P,int *A,int cur) {   /////// 还是要sort一遍,不sort不能按字典序排列 if(cur == n) {for(int i = 0; i < n; ++i) cout << A[i];cout << endl;}else for(int i = 0; i < n; ++i) if(!i || P[i] != P[i-1]) {int c1 = 0, c2 = 0;for(int j = 0; j < cur; ++j) if(A[j] == P[i]) c1++;for(int j = 0; j < n;   ++j) if(P[j] == P[i]) c2++;if(c1 < c2) {A[cur] = P[i];Printf2(n,P,A,cur+1);} }} int main() {//Printf(8,Num,0);int n;cin >> n;for(int i = 0; i < n; ++i) cin >> tmp[i];Printf2(n,tmp,Num,0);return 0;}

0 0
原创粉丝点击