閱讀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 (橋接模式)