本帖最后由 cutecpu 于 2021-3-31 20:47 编辑
Dim x, y, arr, v, r, m, sumx
Sub demo()
x = Range("D3:D10"): y = Range("E2:N2"): arr = Range("E3:N10")
sumx = Application.Sum(x): ReDim v(1 To sumx)
r = 3
Call com(1, 1, 1)
End Sub
Sub com(n As Integer, k As Integer, c As Integer)
If n > UBound(x) Then
Cells(r, "Q").Resize(1, sumx) = v
r = r + 1
Exit Sub
End If
If x(n, 1) = 0 Or c > x(n, 1) Then
Call com(n + 1, 1, 1)
Exit Sub
End If
For i = k To UBound(y, 2) - x(n, 1) + c
If y(1, i) > 0 And arr(n, i) Then
y(1, i) = y(1, i) - 1
m = m + 1
v(m) = arr(n, i)
Call com(n, i + 1, c + 1)
m = m - 1
y(1, i) = y(1, i) + 1
End If
Next
End Sub
祝順心,南無阿彌陀佛!