|
贴一个我自己做的,肯定不是最优。Sub abc(a1 As Double, a2 As Double, a3 As Double, a4 As Double, a5 As Double, a6 As Double, a0 As Double)
Dim i As Byte, j As Byte, k As Byte, l As Byte, m As Byte, n As Byte
Dim gshi As String, zhi As Double, oldzhi As Doubleoldzhi = a0
For i = 0 To 1
For j = 0 To 1
For k = 0 To 1
For l = 0 To 1
For m = 0 To 1
For n = 0 To 1
ai = a1: If i = 0 Then ai = 0
aj = a2: If j = 0 Then aj = 0
ak = a3: If k = 0 Then ak = 0
al = a4: If l = 0 Then al = 0
am = a5: If m = 0 Then am = 0
an = a6: If n = 0 Then an = 0
zhi = Abs(ai + aj + ak + al + am + an - a0)
If zhi < oldzhi Then
oldzhi = zhi
gshi = ai & "+" & aj & "+" & ak & "+" & al & "+" & am & "+" & an & " = " & ai + aj + ak + al + am + an
End If
Next n
Next m
Next l
Next k
Next j
Next i
gshi = Replace(gshi, "+0+", "+", , 4)
gshi = Replace(gshi, "+0=", "=")
If Left(gshi, 2) = "0+" Then gshi = Right(gshi, Len(gshi) - 2)
MsgBox gshi
End Sub
调用:abc 3.1, 1.7, 2, 5.3, 0.9, 7.2, 10 '6个计算数及1个接近数 |
|