递归求集合的所有子集的程序
来源:互联网 发布:创维电视调试端口用途 编辑:程序博客网 时间:2024/05/23 23:00
#include <iostream>
#include <vector>
using namespace std;
//算法描述:
//1、如果遍历完全集,打印出所有被标记为true的元素
//2、否则:
//3、取第depth个元素,即标记为true
//4、继续判断第depth+1个元素
//5、不取第depth个元素,妈标记为false
//6、继续判断第depth+1个元素
//如:集合元素为a,b,c
// 把与元素a对应的标记为true,表示要取出
//判断元素b,进入第一层递归,里面就标记b为取出,再进入递归,标记C为取出,再进入递归,打印a,b,c
//退出上一层递归,标记c不取出,进入第二个递归,由于已经遍历完,打印出ab,
//依此类推
//。。。
void getAllSubset(char str[], int size, vector<bool> bVector, int depth);
int main()
{
char s[] = {'a', 'b', 'c'};
int size = sizeof(s) / sizeof(char);
vector<bool> v(size, false);
getAllSubset(s, size, v, 0);
return 0;
}
void getAllSubset(char str[], int size, vector<bool> bVector, int depth)
{
if (depth == size) //如果把size个数都判断完,就把取出的数打印出来
{
for (int i = 0; i < size; i++)
{
if (bVector[i] != false)
cout << str[i] << " ";
}
cout << endl;
}
else
{
bVector[depth] = true; //取第de pth个数
getAllSubset(str, size, bVector, depth+1); //判断第depth+1个数
bVector[depth] = false; //不取第depth个数
getAllSubset(str, size, bVector, depth+1); //判断第depth+1个数
}
}
#include <vector>
using namespace std;
//算法描述:
//1、如果遍历完全集,打印出所有被标记为true的元素
//2、否则:
//3、取第depth个元素,即标记为true
//4、继续判断第depth+1个元素
//5、不取第depth个元素,妈标记为false
//6、继续判断第depth+1个元素
//如:集合元素为a,b,c
// 把与元素a对应的标记为true,表示要取出
//判断元素b,进入第一层递归,里面就标记b为取出,再进入递归,标记C为取出,再进入递归,打印a,b,c
//退出上一层递归,标记c不取出,进入第二个递归,由于已经遍历完,打印出ab,
//依此类推
//。。。
void getAllSubset(char str[], int size, vector<bool> bVector, int depth);
int main()
{
char s[] = {'a', 'b', 'c'};
int size = sizeof(s) / sizeof(char);
vector<bool> v(size, false);
getAllSubset(s, size, v, 0);
return 0;
}
void getAllSubset(char str[], int size, vector<bool> bVector, int depth)
{
if (depth == size) //如果把size个数都判断完,就把取出的数打印出来
{
for (int i = 0; i < size; i++)
{
if (bVector[i] != false)
cout << str[i] << " ";
}
cout << endl;
}
else
{
bVector[depth] = true; //取第de pth个数
getAllSubset(str, size, bVector, depth+1); //判断第depth+1个数
bVector[depth] = false; //不取第depth个数
getAllSubset(str, size, bVector, depth+1); //判断第depth+1个数
}
}
- 递归求集合的所有子集的程序
- 求集合的所有子集
- 求集合的所有子集
- 求集合的所有子集
- 求集合的所有子集
- 求集合的所有子集
- 递归求解集合的所有子集
- 求集合的所有子集的算法
- 求集合的所有子集的算法
- 用递归方法求集合的子集
- 递归求字母集合的子集C++
- Java递归求某个集合的所有子集组成的集合,即幂集
- 回溯法求集合的所有子集
- 分治法求集合的所有子集
- 求集合的所有子集(续)
- 求一个集合的所有子集
- 求一个集合的所有子集
- 求一个集合的所有子集
- 页面的加载与渲染顺序
- 推荐10款用户界面设计
- 分享一个帮助你快速构建HTML5游戏的javascript类库 - CreateJS
- java集合框架(1) hashMap 简单使用以及深度分析
- 免费素材下载:超酷的简单按钮UI
- 递归求集合的所有子集的程序
- VoltDB内存数据库分析
- USB键盘的键值
- 你做过的最有效的提高编程水平的一件事情是什么
- Debug Error! R6010 -abort() has been called
- 游戏开发
- 第一个测试程序
- 求鞍点的小程序(梁璐)
- AIX系统换页空间