|
'52146,刚才算错了。组合太多,设置了一个小偏差等了一会儿就中断了,估计符合条件的太多。
Option Explicit
Dim output(), cnt
Sub test()
Dim arr, brr, i, result()
ReDim arr([a65536].End(xlUp).Row - 1)
For i = 0 To UBound(arr)
arr(i) = Cells(i + 1, 1)
Next
dsort arr: cnt = 0: ReDim output(1 To 1)
For i = 1 To UBound(arr) + 1
ReDim result(i)
combine_decrease arr, UBound(arr) + 1, result, i, i, [d11].Value
Next
[k:k].ClearContents
If cnt > 0 Then [k1].Resize(UBound(output), 1) = WorksheetFunction.Transpose(output)
End Sub
Function dsort(arr)
Dim i, j, t
For i = LBound(arr) To UBound(arr) - 1
For j = i + 1 To UBound(arr)
If arr(i) < arr(j) Then
t = arr(i): arr(i) = arr(j): arr(j) = t
End If
Next j, i
End Function
Function combine_decrease(arr, start, result, count, num, t)
Dim i
For i = start To count Step -1
result(count - 1) = i - 1
If count > 1 Then
combine_decrease arr, i - 1, result, count - 1, num, t
Else
Dim j, sum
For j = num - 1 To 0 Step -1: sum = sum + arr(result(j)): Next
If sum = t Then
cnt = cnt + 1
ReDim Preserve output(1 To cnt)
For j = num - 1 To 0 Step -1: output(cnt) = output(cnt) & arr(result(j)) & "+": Next
output(cnt) = Left(output(cnt), Len(output(cnt)) - 1) & "=" & t
End If
If sum > t Then Exit For
sum = 0
End If
Next
End Function
|
|