AlgorithmIdea: an element in a set is either in a sub-set or not in a sub-set;
Each time an element is retrieved from the set, the element is placed in the sub-set, and the element continues to be retrieved until the end of the Set;
Delete the element from the sub-set and continue to get the element to know the end of the Set;
When the end of the set is reached, the elements in the subset are output.
C # implementation:
/// <Summary> /// evaluate all subsets of the Set /// </Summary> /// <Param name = "A"> set </param> /// <Param name = "I"> you have selected the nth I-1 element in the set </param> // <Param name = "N"> Number of collection elements </param> /// <Param name = "B"> temporary set, used to output a subset </param> void powerset (INT [] A, int I, int N, list <int> B) {if (I> N) // output the subset element {console. write ("{"); For (Int J = 0; j <B. count; j ++) {If (j <(B. count-1) console. write (B [J] + ","); else console. write (B [J] + "}");} If (B. count = 0) {console. write ("}");} console. writeline ();} else {int x = A [I-1]; B. add (x); // obtain powerset (A, I + 1, n, B); B. remove (x); // homes powerset (A, I + 1, n, B );}}