标签:blog os io for ar 2014 cti div
/******************************************************************** * @file Main_practise.cpp * @date 2014-8-14 * @author Tiger * @brief 练习 * @details 试编写一个递归函数,用来输出n个元素的所有子集。例如, 三个元素{a,b,c}的所有子集是:{}(空集),{a},{b},{c}, {a,b},{a,c},{b,c}和{a,b,c}。 ********************************************************************/ #include <iostream> const int SIZE = 3; void Combination(char str[], int nBeg, int nEnd, bool* bFlag); int main(int argc, const char* argv[]) { char str[SIZE] = {‘a‘, ‘b‘, ‘c‘}; bool bFlag[SIZE] = {false}; Combination(str, 0, SIZE, bFlag); system("pause"); return 0; } void Combination(char str[], int nBeg, int nEnd, bool* bFlag) { if (nBeg == nEnd) { std::cout << "{"; for (int i=0; i<SIZE; ++i) { if (bFlag[i]) { std::cout << str[i]; } } std::cout << "}" << std::endl; } else { bFlag[nBeg] = false; Combination(str, nBeg+1, nEnd, bFlag); bFlag[nBeg] = true; Combination(str, nBeg+1, nEnd, bFlag); } }
标签:blog os io for ar 2014 cti div
原文地址:http://www.cnblogs.com/roronoa-zoro-zrh/p/3913720.html