求集合子集问题
2007-12-25 12:11
267 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//src是源数据集合,currentIndex是在源集合里的当前下标,length为源集合的大小,dest是结果集合,num是结果集合的元素个数,初始化时,结果集合要和源集合的个数相等.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void match(int* src,int currentIndex,int length,int* dest,int num)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(currentIndex == length)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dest[num++] = src[currentIndex++];//采用当前元素
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i =0 ; i < num; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << dest[i]<<" ";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
match(src,currentIndex,length,dest,num);//选用当前元素进行递归
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
match(src,currentIndex,length,dest,num-1);//去除当前元素再进行递归
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
int src[] = {1,2,3};
int* dest = new int[3];打印的结果是:
1
1 2
1 2 3
1 3
2
2 3
3
所以这个算法可以求背包问,加和问题等。凡是与子集合相关的问题,全部可以用这个方法解决。
相关文章推荐
- 集合子集问题:
- 某个集合的子集问题
- 编程拾趣--集合子集问题
- 集合的子集 (其实是组合问题)
- 求集合中M个元素构成的子集问题
- 求一个集合的所有子集 输出一个数所有平方和的情况 背包问题的递归解决
- [置顶] 求一个集合的所有子集问题
- [数字技巧]子集问题(寻找给定集合的所有子集)
- subset 集合子集问题
- N个元素集合的M子集问题
- 求一个集合的所有子集问题
- 求一个集合的所有子集问题
- 求一个集合的全部子集问题
- 求一个集合的所有子集问题
- 求一个集合的所有子集问题
- 傻瓜方法求集合的全部子集问题(java版)
- 求一个集合的所有子集问题
- 求集合所有子集问题
- 傻瓜方法求集合的所有子集问题(java版)
- 集合的子集和集合的全排列问题