九度[1120]全排列

 1 # include<iostream>
 2 # include<cstring>
 3 # include<cstdio>
 4 using namespace std;
 5 char s[200];
 6 void print_permutation(int n,char *a,int cur)
 7 {
 8     int i=0,j=0;
 9     if(cur==n)
10     {
11         for(i=0;i<n;i++)
12             printf("%c",a[i]);
13         printf("\n");
14     }
15     else
16     {
17         for(i=0;i<n;i++)
18         {
19             int ok=1;
20             for(int j=0;j<cur;j++)
21             {
22                 if(a[j]==s[i]) ok=0;
23             }
24             if(ok)
25             {
26                 a[cur]=s[i];
27                 print_permutation(n,a,cur+1);
28             }
29         }
30     }
31 
32 
33 }
34 int main()
35 {
36     while(cin>>s)
37     {
38         int l=strlen(s);
39         int cur=0;
40         char a[200];
41         print_permutation(l,a,cur);
42         cout<<endl;
43     }
44     return 0;
45 }

递归

 1 # include<iostream>
 2 # include<cstdio>
 3 # include<string>
 4 # include<algorithm>
 5 using namespace std;
 6 int main()
 7 {
 8     char str[10];
 9     while(cin>>str)
10     {
11         int l=strlen(str);
12         do
13         {
14                 printf("%s",str);
15             printf("\n");
16         }while(next_permutation(str,str+l));
17     }
18     return 0;
19 }

注意这个函数不能用string

posted @ 2016-02-17 19:24  dreamer123  阅读(372)  评论(0编辑  收藏  举报