各位大侠:
一个客户公司打过来一笔钱,老板想知道这一笔钱是客户公司付的哪几笔业务的款项。比如:客户公司与老板有如下一些业务款项
363.6
2066.4
4132.76
4442.71
829.98
1500.5
3789.55
4412.57
2931.66
7979.75
7062.5
3998.5
11785.01
856
2490
3154.14
187.2
977.48
363.6要求从上述数字中找出和为39475.98 的数字组合来,如何计算?
一个客户公司打过来一笔钱,老板想知道这一笔钱是客户公司付的哪几笔业务的款项。比如:客户公司与老板有如下一些业务款项
363.6
2066.4
4132.76
4442.71
829.98
1500.5
3789.55
4412.57
2931.66
7979.75
7062.5
3998.5
11785.01
856
2490
3154.14
187.2
977.48
363.6要求从上述数字中找出和为39475.98 的数字组合来,如何计算?
Dim iLoop As Integer
ReDim fValue(UBound(sValue))
ReDim bUsed(UBound(sValue))
For iLoop = 0 To UBound(sValue)
fValue(iLoop) = CDbl(sValue(iLoop))
bUsed(iLoop) = False
Next iLoop
End SubFunction FindResult(fTotal As Double) As Boolean
Dim iBit As Integer, fTemp As Single
FindResult = False
Do
iBit = 0
Do While iBit <= UBound(bUsed)
bUsed(iBit) = Not bUsed(iBit)
If bUsed(iBit) Then Exit Do
iBit = iBit + 1
Loop
If iBit > UBound(bUsed) Then Exit Function
fTemp = 0
For iBit = 0 To UBound(bUsed)
If bUsed(iBit) Then
fTemp = fTemp + fValue(iBit)
If Abs(fTemp - fTotal) < 0.01 Then
FindResult = True
Exit Function
End If
End If
Next iBit
Loop
End FunctionFunction GetResult() As String
Dim iLoop As Integer
GetResult = ""
For iLoop = 0 To UBound(bUsed)
If bUsed(iLoop) Then
If GetResult <> "" Then GetResult = GetResult + " + "
GetResult = GetResult & CStr(fValue(iLoop))
End If
Next iLoop
End FunctionPrivate Sub Form_Load()
Const sData = "363.6, 2066.4, 4132.76, 4442.71, 829.98, 1500.5, 3789.55, 4412.57, 2931.66, " & _
"7979.75, 7062.5, 3998.5, 11785.01, 856, 2490, 3154.14, 187.2, 977.48, 363.6"
Dim bResult As Boolean, iCount As Integer
sValue = Split(sData, ",")
Initialize
iCount = 0
Do
bResult = FindResult(39475.98)
If bResult Then
iCount = iCount + 1
Debug.Print "Answer " & iCount & " is : " & GetResult()
End If
Loop While bResult
End
End Sub
Dim iBit As Integer, fTemp As Single
FindResult = False
Do
iBit = 0
Do While iBit <= UBound(bUsed)
bUsed(iBit) = Not bUsed(iBit)
If bUsed(iBit) Then Exit Do
iBit = iBit + 1
Loop
If iBit > UBound(bUsed) Then Exit Function
fTemp = 0
For iBit = 0 To UBound(bUsed)
If bUsed(iBit) Then fTemp = fTemp + fValue(iBit)
Next iBit
If Abs(fTemp - fTotal) < 0.01 Then
FindResult = True
Exit Function
End If
Loop
End Function最终结果是有两种组合(因为363.6这个值出现了两次)。