List All Of The Subsets
2013-01-03 20:30
211 查看
Problem description:Please list all of the subsets of a known set including the empty set.
My idea: one thinking of the algorithm backtracking is to generate a tree of subset and the condition of an element in the super set for a subset is either on or off.Hence we can specialize the subset tree to a binary tree and we print once when we reach the bottom of the tree each time.It is also easy to write a program.
My idea: one thinking of the algorithm backtracking is to generate a tree of subset and the condition of an element in the super set for a subset is either on or off.Hence we can specialize the subset tree to a binary tree and we print once when we reach the bottom of the tree each time.It is also easy to write a program.
#include <stdio.h> #define MAX 1000 int n=3; //the number of the set elements int set[MAX]={1,2,3}; int count=0;//the number of the subset. void DFS(int level); void print_set(); int main() { DFS(0); return 0; } void DFS(int level) { if(level==n) { print_set(); return ; } int save=set[level]; set[level]=0; int index=0; for(index=0;index<2;index++) { DFS(level+1); set[level]=save; } } void print_set() { int index; count++; printf("%d: {",count); for(index=0;index<n;index++) { if(set[index]!=0) printf("%d ",set[index]); } printf("}\n"); }
相关文章推荐
- List All Of The Subsets
- List All Of The Subsets
- Ch8.3: find all the subsets of a set
- List of all the Oracle TIME ZONE - Oracle用到的时区地区名称
- FWNX-creates a linked list of all the nodes at each depth-JAVA VERSION
- Q 4.4 creates a linked list of all the nodes at each depth
- CareerCup Find all the conflicting appointments from a given list of n appointments.
- To find the list of all the available drives on...
- [CrackCode] 4.4 Creates a linked list of all the nodes at each depth of a given tree
- Question[SQL]: How can I list all book with prices greather than the average price of books of the same type?
- list all of the Oracle 12c hidden undocumented parameters
- List All Of The Subset In Another Method
- List All Of The Subset In Another Method
- List All Of The Subset In Another Method
- This list contains all of the known Microsoft Knowledge Base articles, howtos, fixes, hotfixes, webcasts and updates of Microsof
- Retrieving the list of items in a directory all at once
- 69.You need to generate a list of all customer last names with their credit limits from the CUSTOMER
- The history of Graph Store(matrix, adjacency list, and orthogonal list)
- The realization of a simple single linked list【1.8】
- Warning: Permanently added 'git.coding.net,222.186.133.56' (RSA) to the list of known hosts.解决笔记