首页 > 其他 > 详细

九度[1120]全排列

时间:2016-02-17 20:45:13      阅读:282      评论:0      收藏:0      [点我收藏+]
 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

九度[1120]全排列

原文:http://www.cnblogs.com/dreamer123/p/5196371.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!