標題:
如何從一堆數字找出加總符合特定值
[打印本頁]
作者:
VANESSA
時間:
2020-5-27 11:32
標題:
如何從一堆數字找出加總符合特定值
請問如何從下列31個數字,找出加總等於 49177 ?
39824 39615 24616 13572 12760 10530 9963 8090 7290 7240 6300 5940 5822 5814
4536 4342 4114 3816 3150 3078 2520 1944 1881 1560 1232 1170 1062 972 816 729 63318
作者:
ikboy
時間:
2020-5-28 11:22
嘩數值元素有31個, 最大可能組合次數= 2^31, 衹能試試:
Option Explicit
Dim b(), c(1), m&, k&, f$, t(1), dgt As Boolean
Sub zz()
Dim a, i&
Range("d2:d" & Rows.Count).ClearContents
dgt = False
f = "+"
a = Split("39824 39615 24616 13572 12760 10530 9963 8090 7290 7240 6300 5940 5822 5814 4536 4342 4114 3816 3150 3078 2520 1944 1881 1560 1232 1170 1062 972 816 729 63318", " ")
m = UBound(a) + 1
c(1) = 49177
ReDim b(1 To m)
For i = 1 To m
b(i) = a(i - 1) ', 1)
Next i
k = 0: Call dg("", m)
MsgBox c(1) & "=" & Left(c(0), Len(c(0)) - 1)
End Sub
Sub dg(s$, mi&)
Dim j&, jj&, ss$, a, bb()
For j = 0 To 1
If dgt Then Exit Sub
If j Then ss = b(mi) Else ss = ""
If mi > 1 Then Call dg(IIf(ss = "", s, ss & f & s), mi - 1) Else k = k + 1: a = IIf(ss = "", s, ss & f & s)
If Len(a) Then
c(0) = a
a = Split(a, "+")
ReDim bb(UBound(a))
For jj = 0 To UBound(a)
bb(jj) = Val(a(jj))
Next
If Application.Sum(bb) = c(1) Then dgt = True
End If
Next j
End Sub
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)