The power -S,s of a well-known set is a collection of all subsets of the set S, denoted by P (S), for example:
P ({0,1,2}) ={$,{0},{1},{2},{0,1},{ 0,2},{1,2},{0,1,2}};
This reminds me of the binary simulation, what if we now use the algorithm to simulate the set of print powers (except the empty sets)?
Binary simulation plays a very good role, first look at what binary simulation is,
The array is initially 0, loop in the first +1, and carry in binary format, and you end up with the following possibilities:
1 0 0) 0 0
0 1 0) 0 0
1 1 0 0 0 ...
If there are 3 elements, the combination has 7 forms, plus an empty set, that is, all the possibilities of the Power collection!
Full code
#include <iostream>using namespacestd; intMain () {intcarry=1; inta[5]={0};/*State Array*/ intb[5]={0,1,2};/*Collection Array*/ intn=7; while(n>0) { for(intI=0;i<5; i++)/*get a state every time*/{A[i]+=carry; Carry=a[i]/2; A[i]=a[i]%2; if(carry==0) Break; } for(intj=0;j<5; j + +)/*traversing array state, printing power set*/ { if(a[j]==1) cout<<b[j]<<" "; } cout<<Endl; --N; Carry=1;/*Reset*/ } return 0;}
Power set [set theory]