896
技術社區[雲棲]
遞歸求子集
#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