NEs from M mutually different elements, generally use recursive or backtracking algorithms to solve it, this paper is intended to utilize the method of enonomering conversion to achieve this. code show as below
Sub getall (ByVal Num As Integer, Byref X As Variant, Byref Result () AS String, Optional Byref All As long
DIM A () AS STRING, B () AS Integer 'Temporary Array DIM N AS Integer' Array Element DIM I As Long 'Cycle Variable DIM TEMP AS Long' Binary Conversion Intermediate Variable DIM NUM2 AS INTEGER 'Intermediate Count Variable N = Ubound (x) - lbound (x) 1 'array element number if Num> n Then Msgbox "Err!", Vbinformation, "Warning": EXIT SUBREDIM B (0 to n - 1) all = 0for i = 0 To 2 ^ n - 1 'cycle TEMP = inum2 = 0for J = 0 to n - 1' converted to binary b (j) = Temp and 1 '0 or 1TEMP = TEMP / 2IF B (J) = 1 kilnum2 = Num2 1redim Preserve A (1 to Num2) A (Num2) = X (LBound (X) J) end ifXEXTIF NUM2 = Num theall = All 1redim Preserve Result (1 to all) Result (all) = JOIN (A, "," ) 'Results Save Debug.print Result (AL)' Output End ifnextdebug.print "Select" & Num; "in the array of" & n & "elements," & all & "combination!" End Sub
Private submmand1_click () DIM X, I as integerim out () AS Stringx = Array (1, 2, 3, 4, 5, 6, 7, 8) Getall 4, X, Outend Sub
Output:
1, 2, 3, 41, 2, 3, 51, 2, 4, 51, 3, 4, 52, 3, 4, 51, 2, 3, 61, 2, 4, 61, 3, 4, 62, 3, 4, 61, 2, 5, 61, 3, 5, 62, 3, 5, 61, 4, 5, 62, 4, 5, 63, 4, 5, 61, 2, 3, 71, 2, 4, 71, 3, 4, 72, 3, 4, 71, 2, 5, 71, 3, 5, 72, 3, 5, 71, 4, 5, 72, 4, 5, 73, 4, 5, 71, 2, 6, 71, 3, 6, 72, 3, 6, 71, 4, 6, 72, 4, 6, 73, 4, 6, 71, 5, 6, 72, 5, 6, 73, 5, 6, 74, 5, 6, 71, 2, 3, 81, 2, 4, 81, 3, 4, 82, 3, 4, 81, 2, 5, 81, 3, 5, 82, 3, 5, 81, 4, 5, 82, 4, 5, 83, 4, 5, 81, 2, 6, 81, 3, 6, 82, 3, 6, 81, 4, 6, 82, 4, 6, 83, 4, 6, 81, 5, 6, 82, 5, 6, 83, 5, 6, 84, 5, 6, 81, 2, 7, 81, 3, 7, 82, 3, 7, 81, 4, 7, 82, 4, 7, 83, 4, 7, 81, 5, 7, 82, 5, 7, 83, 5, 7, 84, 5, 7, 81, 6, 7, 82, 6, 7, 83, 6, 7, 84, 6, 7, 85, 6, 7, 8 selected from the array of 8 elements, 4 elements, a total of 70 combinations!