next_permutation

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 int perm[1005];
 7 //生成1~n的n!种排列
 8 void permutation(int n)
 9 {
10     for(int i=1;i<=n;i++)
11     {
12         perm[i]=i;
13     }
14     do
15     {
16         for(int j=1;j<=n;j++)
17         {
18             printf("%d ",perm[j]);
19         }
20         printf("
");
21     }while(next_permutation(perm+1,perm+n+1));
22     return ;
23 }
24 
25 int main()
26 {
27     int n;
28     while(scanf("%d",&n)!=EOF)
29     {
30         permutation(n);
31     }
32     return 0;
33 }
34 
35 //手写 生成p~n的全排列
36 
37 /*bool used[1005];
38 int perm[1005];
39 int P;
40 
41 void permutation(int pos,int n)
42 {
43     if(pos==n+1)
44     {
45         for(int j=P;j<=n;j++)
46         {
47             printf("%d ",perm[j]);
48         }
49         printf("
");
50     }
51 
52     for(int i=P;i<=n;i++)
53     {
54         if(!used[i])
55         {
56             perm[pos]=i;
57             used[i]=true;
58             permutation(pos+1,n);
59             used[i]=false;
60         }
61     }
62 
63     return ;
64 }
65 
66 int main()
67 {
68     int N;
69     while(scanf("%d %d",&P,&N)!=EOF)
70     {
71         for(int i=P;i<=N;i++)
72             used[i]=false;
73         permutation(P,N);
74     }
75     return 0;
76 }*/
View Code
原文地址:https://www.cnblogs.com/cyd308/p/4771321.html