NYOJ 19 擅长排列的小明(全排列<next_permutation>)

擅长排列的小明

时间限制:1000 ms  |           内存限制:65535 KB
难度:4
 
描述
小明十分聪明,而且十分擅长排列计算。比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想为难他,在这5个数字中选出几个数字让他继续全排列,那么你就错了,他同样的很擅长。现在需要你写一个程序来验证擅长排列的小明到底对不对。
 
输入
第一行输入整数N(1<N<10)表示多少组测试数据, 每组测试数据第一行两个整数 n m (1<n<9,0<m<=n)
输出
在1-n中选取m个字符进行全排列,按字典序全部输出,每种排列占一行,每组数据间不需分界。如样例
样例输入
2
3 1
4 2
样例输出
1
2
3
12
13
14
21
23
24
31
32
34
41
42
43
资料参考:next_permutation http://www.cplusplus.com/reference/algorithm/next_permutation/
mencpy http://www.cplusplus.com/reference/cstring/memcmp/
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cstdlib>
 5 using namespace std;
 6 int a[10]={1,2,3,4,5,6,7,8,9}, b[10];
 7 int main()
 8 {
 9 int N;
10 scanf("%d", &N);
11 while(N--)
12 {
13 int n, m,i;
14 scanf("%d %d", &n, &m);
15 memcpy(b, a, sizeof(int) * m);
16 for(int i = 0; i < m; i++)
17 printf("%d", a[i]);
18 printf("\n");
19 while (next_permutation(a, a + n))
20 {
21 if(memcmp(b, a, sizeof(int) *m) != 0)
22 {
23 for(i = 0; i < m; i++)
24 printf("%d", a[i]);
25 printf("\n");
26 memcpy(b, a, sizeof(int) * m);
27 }
28 }
29 }
30 return 0;
31 }
32        
View Code

 

posted on 2013-08-29 12:19  落水寒冰  阅读(249)  评论(0编辑  收藏  举报

导航