List All Of The Subsets

简介:
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.
 
 1 #include <stdio.h>
 2  #define MAX 1000
 3  
 4  int n=3;  //the number of the set elements
 5  int set[MAX]={1,2,3};
 6  int count=0;//the number of the subset.
 7  
 8  void DFS(int level);
 9  void print_set();
10  
11  int main()
12  {
13      DFS(0);
14      return 0;
15  }
16  
17  void DFS(int level)
18  {
19      if(level==n)
20      {
21          print_set();
22          return ;
23      }
24      int save=set[level];
25      set[level]=0;
26      int index=0;
27      for(index=0;index<2;index++)
28      {
29          DFS(level+1);
30          set[level]=save;
31      }
32  
33  }
34  
35  void print_set()
36  {
37      int index;
38      count++;
39      printf("%d: {",count);
40      for(index=0;index<n;index++)
41      {
42          if(set[index]!=0) 
43              printf("%d ",set[index]);
44      }
45      printf("}\n");
46  }

 
 
相关文章
|
8月前
|
SQL JSON Java
Java【问题记录 02】对象封装+固定排序+list All elements are null引起的异常处理+Missing artifact com.sun:tools:jar:1.8.0
Java【问题记录 02】对象封装+固定排序+list All elements are null引起的异常处理+Missing artifact com.sun:tools:jar:1.8.0
94 0
成功解决ValueError: ‘usecols‘ must either be list-like of all strings, all unicode, all integers or a ca
成功解决ValueError: ‘usecols‘ must either be list-like of all strings, all unicode, all integers or a ca
为什么有的备份归档日志用list backup of archivelog all查不到,但在第三方备份软件中可以查到
提问:为什么有的备份归档日志用list backup of archivelog all查不到,但在第三方备份软件中可以查到?
|
IDE 开发工具 图形学
|
存储 缓存
memcached实战系列(五)Memcached: List all keys 查询所有的key
memcached可能当时设计的时候就把它定位为内存性的kv结构的缓存系统。所以没有持久化到磁盘的命令,也没有查看所有key的值得命令。可能觉得没必要吧,你要是缓存1个G内存的数据,自己都头大,还敢看。
1137 0
|
7月前
|
安全 Java
java线程之List集合并发安全问题及解决方案
java线程之List集合并发安全问题及解决方案
1099 1