How to get a set of all Subset?

  As we all know, with a collection of n elements with sub 2 ^ n set, and it is not difficult to find, each of which are Subset from 0-2 ^ n-1 each in the format of the binary 0 give up, a choice of The results, as follows: 

  (000) 
  (1) 100 
  (2) 010 
  110 (1,2) 
  001 (3) 
  101 (1,3) 
  (2,3) 011 
  111 (1,2,3) 

  Hence, in accordance with the number of binary conversion, can easily get a set of all Subset, the code below: 

  Sub GETALL (ByVal mycollection As String, ByRef RESULT () As String) 
  Dim x () As String 
  X = Split (Mid (mycollection, 2, Len (mycollection) - 2), "") 

  Dim A () As String, b () As Integer 'temporary array 
  Dim n As Integer 'set the number of elements 
  Dim i As Long 'cycle variables 
  Dim num As Integer 'Subset of the number of elements 
  Dim TEMP As Integer 'conversion between binary variables 
  N = UBound (x) + 1 
  ReDim b (0 To n - 1) 
  ReDim RESULT (2 ^ n - 1) 
  Debug.Print "pools" & & mycollection "Subset of a total of" & 2 ^ n & "months!" 

  For i = 0 To 2 ^ n - 1 
  TEMP = i 
  Num = 0 
  For j = 0 To n - 1 'to binary 
  B (j) = 0 or 1 TEMP And 1 
  TEMP TEMP = \ 2 
  If b (j) = 1 Then 
  Num num + = 1 
  ReDim Preserve A (1 To num) 
  A (num) = x (j) 
  End If 
Next

  RESULT (i) = "(" Join & (A, "") & ")" 'Save 
  Debug.Print RESULT (i) 'output 

Next
  MsgBox "OK" 
  End Sub 

  Private Sub Command1_Click () 
  Dim S () As String 
  GETALL "(1,2,3,4,5,6)," S 
  End Sub 

  Output: 

  Set (1,2,3,4,5,6) Subset of a total of 64! 
  () 
  (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) 
  (3,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) 
  (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) 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Releated Articles

  • Popuklar Articles

0 Comments to “How to get a set of all Subset?”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.