记录下顺序全排列与逆序全排列的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;
}