从数组中取出n个元素的所有组合(递归实现)

转载自http://www.cnblogs.com/shuaiwhu/archive/2012/04/27/2473788.html 作者:Microgoogle


今天在做POJ 1753时,需要枚举一个数组中所有组合。之前也遇到过类似的问题,如求从n个数组任意选取一个元素的所有组合都是想起来比较简单,但是设计成算法却颇费周折。
如数组为{1, 2, 3, 4, 5, 6},那么从它中取出3个元素的组合有哪些,取出4个元素的组合呢?比如取3个元素的组合,我们的思维是:取1、2,然后再分别取3,4,5,6;取1、3,然后再分别取4,5,6;......取2、3,然后再分别取4,5,5;......这样按顺序来,就可以保证完全没有重复。这种顺序思维给我们的启示便是这个问题可以用递归来实现,但是仅从上述描述来看,却无法下手。我们可以稍作改变:1.先从数组中A取出一个元素,然后再从余下的元素B中取出一个元素,然后又在余下的元素C中取出一个元素2.按照数组索引从小到大依次取,避免重复

依照上面的递归原则,我们可以设计如下的算法,按照索引从小到大遍历:

 //arr为原始数组
 //start为遍历起始位置
 //result保存结果,为一维数组
 //count为result数组的索引值,起辅助作用
 //NUM为要选取的元素个数
 //arr_len为原始数组的长度,为定值
 void combine_increase(int* arr, int start, int* result, int count, const int NUM, const int arr_len)
 {
   int i = 0;
   for (i = start; i < arr_len + 1 - count; i++)
   {
     result[count - 1] = i;
     if (count - 1 == 0)
     {
       int j;
       for (j = NUM - 1; j >= 0; j--)
         printf("%d\t",arr[result[j]]);
       printf("\n");
     }
     else
       combine_increase(arr, i + 1, result, count - 1, NUM, arr_len);
   }
 }



当然,我们也可以按照索引从大到小进行遍历:


//arr为原始数组
 //start为遍历起始位置
 //result保存结果,为一维数组
 //count为result数组的索引值,起辅助作用
 //NUM为要选取的元素个数
 void combine_decrease(int* arr, int start, int* result, int count, const int NUM)
 {
   int i;
   for (i = start; i >=count; i--)
   {
     result[count - 1] = i - 1;
     if (count > 1)
     {
       combine_decrease(arr, i - 1, result, count - 1, NUM);
     }
     else
     {
       int j;
       for (j = NUM - 1; j >=0; j--)
     printf("%d\t",arr[result[j]]);
       printf("\n");
     }
   }
 }
测试代码:
#include <stdio.h>
 
 int main()
 {
   int arr[] = {1, 2, 3, 4, 5};
   int num = 4;
   int result[num];
 
   combine_increase(arr, 0, result, num, num, sizeof(arr)/sizeof(int));
   printf("分界线\n");
   combine_decrease(arr, sizeof(arr)/sizeof(int), result, num, num);
   return 0;
 }

结果:

1	2	3	4	
1	2	3	5	
1	2	4	5	
1	3	4	5	
2	3	4	5	
分界线
5	4	3	2	
5	4	3	1	
5	4	2	1	
5	3	2	1	
4	3	2	1	


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值