【转载】原地址:http://www.cnblogs.com/shuaiwhu/archive/2012/04/27/2473788.html 原作者:NULL00
这里只贴一下思路和部分代码
我们可以稍作改变:
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);
}
}