全排列和组合的实现算法

一 全排列算法原理和实现
全排列是将一组数按一定顺序进行排列,如果这组数有n个,那么全排列数为n!个。现以{1, 2, 3, 4, 5}为 例说明如何编写全排列的递归算法。
1、首先看最后两个数4, 5。 它们的全排列为4 5和5 4, 即以4开头的5的全排列和以5开头的4的全排列。由于一个数的全排列就是其本身,从而得到以上结果。
2、再看后三个数3, 4, 5。它们的全排列为3 4 5、3 5 4、 4 3 5、 4 5 3、 5 3 4、 5 4 3 六组数。即以3开头的和4,5的全排列的组合、以4开头的和3,5的全排列的组合和以5开头的和3,4的全排列的组合.从而可以推断,设一组数p = {r1, r2, r3, ... ,rn}, 全排列为perm(p),pn = p - {rn}。因此perm(p) = r1perm(p1), r2perm(p2), r3perm(p3), ... , rnperm(pn)。当n = 1时perm(p} = r1。

为了更容易理解,将整组数中的所有的数分别与第一个数交换,这样就总是在处理后n-1个数的全排列。

1.  整形数组

[cpp]  view plain copy
  1. #include <stdio.h>      
  2.     
  3. int n = 0;      
  4.     
  5. void swap(int *a, int *b)     
  6. {         
  7.     int m;     
  8.     if(*a != *b)    
  9.     {    
  10.         m = *a;         
  11.         *a = *b;         
  12.         *b = m;     
  13.     }    
  14. }      
  15. void perm(int list[], int k, int m)     
  16. {         
  17.     int i;         
  18.     if(k >/* == */ m)         
  19.     {              
  20.         for(i = 0; i <= m; i++)                 
  21.             printf("%d ", list[i]);             
  22.         printf("\n");             
  23.         n++;         
  24.     }         
  25.     else         
  26.     {             
  27.         for(i = k; i <= m; i++)             
  28.         {                 
  29.             swap(&list[k], &list[i]);                 
  30.             perm(list, k + 1, m);                 
  31.             swap(&list[k], &list[i]);             
  32.         }         
  33.     }     
  34. }     
  35. int main()     
  36. {         
  37.     int list[] = {1, 2, 3, 4, 5};         
  38.     perm(list,0,sizeof(list) / sizeof(int) - 1);         
  39.     printf("total:%d\n", n);         
  40.     return 0;     
  41. }    

2. 字符串

方法一:

[cpp]  view plain copy
  1. #include <stdio.h>    
  2.    
  3. inline void Swap(char& a, char& b)     
  4. {// 交换a和b     
  5.     char temp = a;     
  6.     a = b;     
  7.     b = temp;     
  8. }     
  9.     
  10. void Perm(char list[], int k, int m)     
  11. //生成list [k:m ]的所有排列方式     
  12.     int i;     
  13.     if (k ==/*>*/ m) {//输出一个排列方式     
  14.         for (i = 0; i <= m; i++)     
  15.             putchar(list[i]);     
  16.         putchar('\n');     
  17.     }     
  18.     else // list[k:m ]有多个排列方式     
  19.         // 递归地产生这些排列方式     
  20.         for (i=k; i <= m; i++) {     
  21.             Swap (list[k], list[i]);     
  22.             Perm (list, k+1, m);     
  23.             Swap (list [k], list [i]);     
  24.         }     
  25. }     
  26.     
  27. int main()     
  28. {     
  29.     char s[]="123";     
  30.     Perm(s, 0, 2);     
  31.     return 0;     
  32. }     

方法二:

从集合中依次选出每一个元素,作为排列的第一个元素,然后对剩余的元素进行全排列,如此递归处理,从而得到所有元素的全排列。以对字符串abc进行全排列为例,我们可以这么做:以abc为例
固定a,求后面bc的排列:abc,acb,求好后,a和b交换,得到bac
固定b,求后面ac的排列:bac,bca,求好后,c放到第一位置,得到cba
固定c,求后面ba的排列:cba,cab。代码可如下编写所示:

[cpp]  view plain copy
  1. void swap(char &pCh,char &pBegin)  
  2. {  
  3.     char tmp;  
  4.     tmp = pCh;  
  5.     pCh = pBegin;  
  6.     pBegin = tmp;  
  7. }    
  8. void Permutation(char* pStr, char* pBegin)//pStr与pBegin相同  
  9. {    
  10.     if(!pStr || !pBegin)    
  11.         return;    
  12.         
  13.     if(*pBegin == '\0')    
  14.     {    
  15.         printf("%s\n", pStr);    
  16.     }    
  17.     else    
  18.     {    
  19.         for(char* pCh = pBegin; *pCh != '\0'; ++ pCh)    
  20.         {    
  21.             // swap pCh and pBegin       
  22.             swap(*pCh,*pBegin);  
  23.             Permutation(pStr, pBegin + 1);      
  24.             // restore pCh and pBegin     
  25.             swap(*pCh,*pBegin);  
  26.         }    
  27.     }    
  28. }    
  29. int main()     
  30. {     
  31.     char str[] = "123";  
  32.     Permutation(str,str);  
  33.     return 0;     
  34. }     

二 组合的原理与算法

题目:输入一个字符串,输出该字符串中字符的所有组合。举个例子,如果输入abc,它的组合有 、abcabacbcabc

分析:假设我们想在长度为n的字符串中求m个字符的组合。我们先从头扫描字符串的第一个字符。针对第一个字符,我们有两种选择:一是把这个字符放到组合中去,接下来我们需要在剩下的n-1个字符中选取m-1个字符;二是不把这个字符放到组合中去,接下来我们需要在剩下的n-1个字符中选择m个字符。这两种选择都很容易用递归实现。下面是这种思路的参考代码:

实现方法一:

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <vector>  
  3. using namespace std;  
  4.   
  5. void _Combination(const char* string, vector<char>& result)  
  6. {  
  7.     if(*string == '\0')  
  8.     {  
  9.         vector<char>::iterator iter = result.begin();  
  10.         for(; iter < result.end(); ++ iter)  
  11.             printf("%c", *iter);  
  12.         printf("\n");  
  13.           
  14.         return;  
  15.     }  
  16.    
  17.     result.push_back(*string);  
  18.     _Combination(string + 1, result);  
  19.     result.pop_back();  
  20.    
  21.     _Combination(string + 1, result);  
  22. }  
  23.   
  24. void Combination(char* string)  
  25. {  
  26.     if(string == NULL)  
  27.         return;   
  28.   
  29.     vector<char> result;  
  30.       
  31.     _Combination(string, result);  
  32.       
  33. }  
  34.    
  35. void main()  
  36. {  
  37.     char *pString = "abc";  
  38.     Combination(pString);  
  39. }  
实现方法二:
[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <vector>  
  3. using namespace std;  
  4. void _Combination(const char* string, int number, vector<char>& result)  
  5. {  
  6.     if(number == 0)  
  7.     {  
  8.         vector<char>::iterator iter = result.begin();  
  9.         for(; iter < result.end(); ++ iter)  
  10.             printf("%c", *iter);  
  11.         printf("\n");  
  12.           
  13.         return;  
  14.     }  
  15.    
  16.     if(*string == '\0')  
  17.         return;  
  18.    
  19.     result.push_back(*string);  
  20.     _Combination(string + 1, number - 1, result);  
  21.     result.pop_back();  
  22.    
  23.     _Combination(string + 1, number, result);  
  24. }  
  25.   
  26. void Combination(char* string)  
  27. {  
  28.     if(string == NULL)  
  29.         return;   
  30.     int length = strlen(string);  
  31.     vector<char> result;  
  32.     for(int i = 0; i <= length; ++ i)  
  33.     {  
  34.         _Combination(string, i, result);  
  35.     }  
  36. }   
  37. void main()  
  38. {  
  39.     char *pString = "abc";  
  40.     Combination(pString);  
  41. }  
实现方法三:
[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <iostream.h>  
  3.   
  4.   
  5. int combine(int a[],int n,int m)  
  6. {  
  7.     m = m > n ?n:m;  
  8.   
  9.     int * order =new int [m +1];  
  10.   
  11.     for(int i = 0; i <= m; i ++)  
  12.         order[i] = i - 1;  
  13.   
  14.     int count = 0;  
  15.   
  16.     int k = m;  
  17.   
  18.     bool flag = true;  
  19.   
  20.     while(order[0] == -1)  
  21.     {  
  22.         if(flag)  
  23.         {  
  24.             for(i = 1; i <= m; i ++)  
  25.                 cout<<a[order[i]]<<" ";  
  26.             cout<<endl;  
  27.             count ++;  
  28.             flag =false;  
  29.         }  
  30.   
  31.         order[k] ++;  
  32.   
  33.         if(order[k] == n)  
  34.         {  
  35.             order[k --] = 0;  
  36.             continue;  
  37.         }  
  38.         if(k < m)  
  39.         {  
  40.             order[++ k] = order[k - 1];  
  41.             continue;  
  42.         }  
  43.         if(k == m)  
  44.             flag = true;  
  45.     }  
  46.   
  47.     delete[] order;  
  48.   
  49.     return count;  
  50. }  
  51.   
  52. void main()  
  53. {  
  54.     int a[] = {1,2,3,4,5,6,7};  
  55.   
  56.     int len = sizeof(a) / sizeof(int);  
  57.   
  58.     int sum = 0;  
  59.   
  60.     for(int i = 0;i <= len;i ++)  
  61.         sum += combine(a,len,i);  
  62.   
  63.     printf("sum = %d\n",sum);  
  64. }  


注明:以上部分转载http://zhedahht.blog.163.com/blog/static/2541117420114172812217/,在此表示感谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值