全排列

全排列

全排列

递归实现
·与第一个交换
·递归

*pch 与 *begin 之间的交换与复原
for (char* pch = pbegin; *pch != '\0'; ++pch)
{
 char temp = *pch;
 *pch = *pbegin;
 *pbegin = temp;

 permutation(pstr, pbegin + 1);

 temp = *pch;
 *pch = *pbegin;
 *pbegin = temp;
}
http://zhedahht.blog.163.com/blog/static/254111742007499363479/

 1  #include  < iostream >
 2  using   namespace  std;
 3 
 4  void  permutation( char *  pstr,  char *  pbegin)
 5  {
 6       if  (pstr  ==   0   ||  pbegin  ==   0 )
 7      {
 8           return ;
 9      }
10       if  ( * pbegin  ==   ' \0 ' )
11      {
12          cout  <<  pstr  <<  endl;
13      }
14       else
15      {
16           for  ( char *  pch  =  pbegin;  * pch  !=   ' \0 ' ++ pch)
17          {
18               char  temp  =   * pch;
19               * pch  =   * pbegin;
20               * pbegin  =  temp;
21 
22              permutation(pstr, pbegin  +   1 );
23 
24              temp  =   * pch;
25               * pch  =   * pbegin;
26               * pbegin  =  temp;
27          }
28      }
29  }
30 
31  void  perm( char *  str)
32  {
33      permutation(str, str);
34  }
35 
36  int  main()
37  {
38       char  s[ 4 =   " abc " ;
39      perm(s);
40  }

 


你可能感兴趣的:(全排列)