第四题:如果一个字符数组中有重复的字符 如"abbcd" 那么如何编程打印其全排列

 1   #include <stdio.h>
 2   
 3  void permutation(char s[], int b, int e)
 4  {
 5      if( (0 <= b) && (b <= e) )
 6     {
 7          if( b == e )
 8          {
 9              printf("%s\n", s);
10          }
11          else
12          {
13              int i = 0;
14              
15              for(i=b; i<=e; i++)
16              {
17                  char c = s[b];
18                  s[b] = s[i];
19                  s[i] = c;
20                  
21                  permutation(s, b+1, e);
22                  
23                  c = s[b];
24                  s[b] = s[i];
25                 s[i] = c;
26              }
27          }
28      }
29 }
30  
31  int main()
32  {
33     char s[] = "abcd";
34     
35      permutation(s, 0, 3);
36     
37     return 0;
38 }

 

posted @ 2013-01-31 10:57  海 哥  阅读(321)  评论(0编辑  收藏  举报