阅读896 返回首页    go 阿里云 go 技术社区[云栖]


递归求子集

#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;
}

最后更新:2017-04-02 15:15:29

  上一篇:go Activity完全加载完毕后的回调函数
  下一篇:go Bridge Pattern (桥接模式)