c++递归求子集

#include <iostream>
 
using namespace std;
 
//作为全局变量
char set[] = {'a', 'b', 'c', 'd'};
bool isVisited[4] = {false};
int size = 4;
 
 
void getAllSubset(int depth)
{
    if (depth == size)//如果遍历完全集,打印出所有被标记为true的元素
    {
        for (int i = 0; i < size; i++)
        {
            if (isVisited[i])
                cout << set[i];
        }
        cout << endl;
    }
    else
    {
        isVisited[depth] = true;
        getAllSubset(depth+1);//寻找下一个元素
        isVisited[depth] = false;
        getAllSubset(depth+1);
    }
}
 
int main()
{
 
    getAllSubset(0);
 
    cin.get();
    return 0;
}

 

 

 

posted @ 2020-12-04 10:10  Python研究者  阅读(529)  评论(0编辑  收藏  举报