幸好只有10多个 用递归就行了 例子,只是说一下意思 function getsum(a()) dim count dim b() if sum(a)>total count=ubound(a) redim b(count-1) for i=1 to count 设置数组b getsum(b) next i end function
10张单,不过只有1024种组合方式而已,不困难。Dim A(9) As Double '储存这10张单的“合计数” Dim Sum As Double '储存“总数” Dim I,J,K as Integer Dim TmpSum as Double Dim TmpStr as StringFor I = 1 To 1024 TmpSum = 0 TmpStr = "" K = I For J = 0 To 9 TmpSum = TmpSum + (K Mod 2) * A(J) K = Int(K / 2) TmpStr = TmpStr + (IIf((K Mod 2 = 1), Trim(Str(J)) + " ", "")) Next If TmpSum = Sum Then MsgBox "Found it:" + TmpStr End If Next
do rnd1=int(rnd*10) x=0 for i=0 to rnd1 x=x+ss(rnd*10) if x=ucount then stop next i loop while true
哟,不好意思,写错一点点小东西。 我上边的代码改成这样:For I = 1 To 1024 TmpSum = 0 TmpStr = "" K = I For J = 0 To 9 TmpSum = TmpSum + (K Mod 2) * A(J) '下边这两句写反位置了,呵呵 TmpStr = TmpStr + (IIf((K Mod 2 = 1), Trim(Str(J)) + " ", "")) K = Int(K / 2) Next If TmpSum = Sum Then MsgBox "Found it:" + TmpStr End If Next
用递归就行了
例子,只是说一下意思
function getsum(a())
dim count
dim b()
if sum(a)>total
count=ubound(a)
redim b(count-1)
for i=1 to count
设置数组b
getsum(b)
next i
end function
Dim Sum As Double '储存“总数”
Dim I,J,K as Integer
Dim TmpSum as Double
Dim TmpStr as StringFor I = 1 To 1024
TmpSum = 0
TmpStr = ""
K = I
For J = 0 To 9
TmpSum = TmpSum + (K Mod 2) * A(J)
K = Int(K / 2)
TmpStr = TmpStr + (IIf((K Mod 2 = 1), Trim(Str(J)) + " ", ""))
Next
If TmpSum = Sum Then
MsgBox "Found it:" + TmpStr
End If
Next
总的意思就是如果n个的和>total
就测试n次n-1个,n初始为总个数了
你自己写吧
rnd1=int(rnd*10)
x=0
for i=0 to rnd1
x=x+ss(rnd*10)
if x=ucount then stop
next i
loop while true
我上边的代码改成这样:For I = 1 To 1024
TmpSum = 0
TmpStr = ""
K = I
For J = 0 To 9
TmpSum = TmpSum + (K Mod 2) * A(J)
'下边这两句写反位置了,呵呵
TmpStr = TmpStr + (IIf((K Mod 2 = 1), Trim(Str(J)) + " ", ""))
K = Int(K / 2) Next
If TmpSum = Sum Then
MsgBox "Found it:" + TmpStr
End If
Next