1 产生n个数的全排列
输入 3 1 0 2
输出 0 1 2 0 2 1 1 0 2 1 2 0 2 0 1 2 1 0
#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){
int n;
while(scanf("%d",&n)&&n){
int a[1000];
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);//可以自行测试一下删除后的结果;next_permutation(a,a+n)作用于排好序的数组/对列中
do{
for(int i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}while(next_permutation(a,a+n));
}
return 0;
}
函数next_permutation(参数1,参数2)的双参数调用方式,默认是按照字典序产生排列的,并且是从数组中当前的字典序开始依次增大直至到最大字典序。
2 题目就是有一个数n(0<n<10),写出1到n的全排列
输入 多组(m)数据,对每组数n(0<n<10),写出1到n的全排列
输入 2 2 3 (两组数据 分别为2 ,3那么就是寻找1,2 和1,2,3的全排列)
输出
12
21
123
132
213
231
312
321
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
Int i,m,n;
cin>>m;
while(m--)
{
cin>>n;
int a[n];
for(i=0;i<n;i++)
a[i]=i+1;
do
{
for(i=0;i<n;i++)
cout<<a[i];
cout<<endl;
}
while (next_permutation(a,a+n));//排列组合函数
}
return 0;
}
3 第一行输入整数N(1<N<10)表示多少组测试数据,每组测试数据第一行两个整数 n m (1<n<9,0<m<=n)
样例输入
3
3 1
4 2
3 3
样例输出
1
2
3
12
13
14
21
23
24
31
32
34
41
42
43
123
132
213
231
312
321
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
int m, n, i, b;
cin >> b;
while (b--)
{
string a, s1, s2;
cin >> n >> m;
for (i = 0; i<n; i++)
a += i + '1';
s1 = a.substr(0, m);
cout << s1 << endl;
// next_permutation(a.begin(), a.end());//先给s2一个初值
do
{
s2 = a.substr(0, m);
if (s1 != s2)
{
cout << s2 << endl;
s1 = s2;
}
} while (next_permutation(a.begin(), a.end()));
}
return 0;
}