输出一个序列的所有子集
思路一:递归
- #include <iostream>
- #define N 10
- using namespace std;
- void all_subset(int *arr, int size, int *judge, int depth)
- {
- if(depth == size)
- {
- for(int i = 0; i < size; i ++)
- if(judge[i])
- cout << arr[i] << " ";
- cout << endl;
- }
- else
- {
- judge[depth] = 0;
- all_subset(arr, size, judge, depth + 1);
- judge[depth] = 1;
- all_subset(arr, size, judge, depth + 1);
- }
- }
- int main()
- {
- int s[] = {1, 2, 3, 4};
- int size = sizeof(s) / sizeof(int);
- int judge[N] = {0};
- all_subset(s, size, judge, 0);
- system("pause");
- return 0;