a列放数据,b1放要求的和,c列出结果
Public arr1, j As Long, z%, k%
Public arr2(1 To 65536) As String
Sub recu()
Sheet1.Range("C:C").ClearContents
Application.ScreenUpdating = False
tt = Timer
j = 0
z = [A65536].End(xlUp).Row
arr1 = WorksheetFunction.Transpose(Range("a1", Cells(z, 1)))
k = Cells(1, 2)
xi 1, 0, ""
If j > 0 Then
Range(Cells(1, 3), Cells(j, 3)) = WorksheetFunction.Transpose(arr2)
End If
MsgBox "找到 " & j & " 个解! 花费" & Format(Timer - tt, "0.00") & "秒"
Application.ScreenUpdating = True
End Sub
Sub xi(i%, x%, y$)
If x + arr1(i) = k Then
j = j + 1
arr2(j) = y & arr1(i)
End If
If i < z And x < k Then '
If x + arr1(i) < k Then
xi i + 1, x + arr1(i), y & arr1(i) & ","
End If
xi i + 1, x, y
End If
End Sub