VBA introductory to advanced knowledge of code commonly used summary 46

Recursive algorithm 46 sets of combination
203, recursive basis
1 What is recursion?
Recursive call their own is their own.
2 recursive What are the benefits?
Simplify the code, make the program more simple. Especially in the case where the number of layers indefinite cycle can be greatly simple code.
3 recursive any harm?
Because the use of recursion in temporary storage will have a lot of information "stack" (according to FIFO store information), so the effect is running low, it is generally not recommended to use a recursive design procedure.
204, an example of
the factorial (4 * 3 * 2 * 1 = 24) 4
Sub General procedure ()
Dim K, X
K = 1
the For X = 1 the To the Step 4 -1
K = K * X
the Next X
MsgBox K
End Sub

Recursive method for calculating the factorial:
Sub recursion. 1 ()
MsgBox Jiecheng (. 5)
End Sub

'函数法
Function Jiecheng (n As Integer) As Integer
If n = 1 Then
Jiecheng = 1
Else
Jiecheng Jiecheng = n * (n - 1)
End If
End Function

Combination problem:
Here Insert Picture Description
Option-Explicit
Dim arr1 (the To 100. 1, the To. 1. 1) 'the results are grouped in arr1
Dim k As Integer' as the number of rows of filled arr1
Sub composition ()
Dim ARR
K = 0
the Erase arr1
ARR the Range = (. "A2: A" & the Range ( "a65536") End (xlUp) .Row)
zuhe ARR,. 1, "", 0
the Range ( "C2"). the Resize (100) = ""
the Range ( "C2 ") .Resize (k) = arr1
End Sub

Zuhe Sub (ARR, X, sr, y)
'ARR the source array into the child process
' x recursive index
'string connected sr
number y connected to'
the If y = [B2] the Then
K = K +. 1
of arr1 ( K,. 1) SR =
the Exit Sub
End the If

If x < UBound(arr) + 1 Then
  zuhe arr, x + 1, sr & arr(x, 1), y + 1
  zuhe arr, x + 1, sr, y
End If

End Sub

Summation
Here Insert Picture Description
Option-Explicit
Dim arr1 (the To 10000. 1, the To. 1. 1) of As String 'arr1 in the expression in formula
Dim k As Integer' as the number of rows of filled arr1
Dim of As Integer G, H of As Integer
Dim ARR
Dim K1
Sub combination of ()
K = 0
Dim T
T = the Timer
the Erase of arr1
ARR = the Range (. "A2: A" & the Range ( "a65536") End (xlUp) .Row)
G = [B2]
H = [C2]
zuhe. 1 , 0, "", 0
. "the Resize (K) = of arr1 the Range (" D2)
[E1] = K1
MsgBox! "find" & k & "a solution takes" & Format (Timer - t, "0.00") & "seconds"
End Sub

Sub zuhe(x%, z%, sr$, gg As Byte)
If z + arr(x, 1) = h And gg = g - 1 Then
k = k + 1
arr1(k, 1) = sr & arr(x, 1) & “=” & h
Exit Sub
End If
If x < UBound(arr) And z < h Then
If z + arr(x, 1) < h Then
zuhe x + 1, z + arr(x, 1), sr & arr(x, 1) & “+”, gg + 1
End If
zuhe x + 1, z, sr, gg
End If
End Sub

Sub 循环法()
Dim x As Integer
Dim y As Integer
Dim z As Integer
Dim t
Dim arr(1 To 1000, 1 To 1) As String
Dim q As Long, q1 As Long
t = Timer
For x = 1 To 97
For y = x + 1 To 98
For z = y + 1 To 99
q1 = q1 + 1
If x + y + z = 54 Then
q = q + 1
arr1(q, 1) = x & “+” & y & “+” & z & “=54”
End If
Next z, y, x
Range(“e2”).Resize(10000) = “”
Range(“e2”).Resize(q) = arr1
MsgBox Timer - t
End Sub

Published 47 original articles · won praise 0 · Views 129

Guess you like

Origin blog.csdn.net/tiansdk320/article/details/104365885