---
title: "求集合子集问题"
author: "holoblog"
date: "2007-12-25T12:11:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2011-11-11T11:23:20Z"
id: 1252029
url: "https://holoblog.iteye.com/blog/1252029"
---

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//src是源数据集合,currentIndex是在源集合里的当前下标,length为源集合的大小,dest是结果集合,num是结果集合的元素个数,初始化时,结果集合要和源集合的个数相等.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)voidmatch(int\*src,intcurrentIndex,intlength,int\*dest,intnum)  
![](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(inti=0;i&lt;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&lt;&lt;dest\[i]&lt;&lt;"";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cout&lt;&lt;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

所以这个算法可以求背包问，加和问题等。凡是与子集合相关的问题，全部可以用这个方法解决。