We all know that a collection of N elements has 2 ^ n sets, and it is not difficult to find that each subset is from 0 to 2 ^ n-1 each number of binary formats 0 abandoned, 1 selected As a result, as shown below:
{} 000
{1} 100
{2} 010
{1,2} 110
{3} 001
{1,3} 101
{2,3} 011
{1,2,3} 111
So according to the binary conversion of the numbers, you can easily get all the subsets of a collection, the code is as follows:
Sub getAll (byval mycollection as string) DIM x () as stringx = split (MID (MyCollection, 2, Len (MyCollection) - 2), ",")
DIM A () AS STRING, B () AS INTEGER 'Temporary Array DIM N AS Integer' Collection Element DIM I as Long 'Cycle Variable DIM NUM AS INTEGER' Subset Element DIM TEMP AS INTEGER 'Binary Conversion Intermediate Variable N = ubound (x) 1redim b (0 to n - 1) Redim Result (2 ^ n - 1) Debug.print "Collection" & mycollection & "Commando Collection" & 2 ^ n & "!"
For i = 0 to 2 ^ n - 1TEMP = inum = 0for J = 0 to n - 1 'converted to binary b (j) = Temp and 1' 0 or 1TEMP = TEMP / 2IF B (J) = 1 dam = Num 1redim preserve a (1 to num) A (NUM) = x (j) end ifnext
Result (i) = "{" & join (a, ",") & "}" results save debug.print result (i) 'output
NexTMSGBOX "OK" End Sub
Private Sub Command1_Click () DIM S () AS StringgetAll "{1, 2, 3, 4, 5, 6}", Send Sub
Output:
Collection {1, 2, 3, 4, 5, 6} 64 sets of copies! {} {1} {2} {1,2} {3} {1,3} {2,3} {1,2,3} {4} {1,4} {2,4} {1,2 , 4} {3,4} {1,3,4} {2, 3, 4} {1, 2, 3, 4} {5} {1,5} {2,5} {1,2,5 } {3,5} {1,3,5} {2,3,5} {1,2,3,5} {4,5} {1,4,5} {2,4,5} {1 , 2, 4, 5} {1,3,4,5} {2, 3, 4, 5} {1, 2, 3, 4, 5} {6} {1,6 } {2,6} {1,2,6} {3,6} {1,3,6} {2,3,6} {1,2,3,6} {4,6} {1,4 , 6} {2,4,6} {1,2,4,6} {3,4,6} {1,3,4,6} {2,3,4,6} {1,2,3 4, 6} {5,6} {1,5,6} {2,5,6} {1,2,5,6} {3,5,6} {1,3,5,6} { 2, 3, 5, 6} {1,2,3,5,6} {4,5,6} {1,4,5,6} {2,4,5,6} {1,2,4 5,6} {3,4,5,6} {1,3,4,5,6} {2,3,4,5,6} {1,2,3,4,5,6}