|
6#
楼主 |
发表于 2005-11-11 20:18:00
|
只看该作者
Option ExplicitDim answ As String
Const a1 = 9999, b1 = 9, c1 = 7, d = 4Public Sub test()
answ = ""
Call fj
Debug.Print "The Best step is:" & answ
End SubPublic Function fj(Optional str As String, Optional a As Integer, Optional b As Integer, Optional c As Integer) As String
If a = 0 And b = 0 And c = 0 Then a = a1
If a = d Or b = d Or c = d Then
If answ = "" Or Len(Mid(str, 2) & vbNewLine & a & " " & b & " " & c) < Len(answ) Then answ = Mid(str, 2) & vbNewLine & a & " " & b & " " & c
'Debug.Print Mid(str, 2) & vbNewLine & a & " " & b & " " & c
Else
If InStr(1, str, a & " " & b & " " & c) = 0 Then
If a > 0 And b < b1 Then fj str & vbNewLine & a & " " & b & " " & c, IIf(a - b1 + b > 0, a - b1 + b, 0), IIf(a - b1 + b > 0, b1, a + b), c
If b > 0 And a < a1 Then fj str & vbNewLine & a & " " & b & " " & c, IIf(a - a1 + b > 0, a1, a + b), IIf(a + b > a1, a - a1 + b, 0), c
If a > 0 And c < c1 Then fj str & vbNewLine & a & " " & b & " " & c, IIf(a + c > c1, a - c1 + c, 0), b, IIf(a - c1 + c > 0, c1, a + c)
If c > 0 And a < a1 Then fj str & vbNewLine & a & " " & b & " " & c, IIf(a - a1 + c > 0, a1, a + c), b, IIf(a + c > a1, a - a1 + c, 0)
If b > 0 And c < c1 Then fj str & vbNewLine & a & " " & b & " " & c, a, IIf(b - c1 + c > 0, b - c1 + c, 0), IIf(b - c1 + c > 0, c1, b + c)
If c > 0 And b < b1 Then fj str & vbNewLine & a & " " & b & " " & c, a, IIf(b - b1 + c > 0, b1, b + c), IIf(b - b1 + c > 0, b - b1 + c, 0)
End If
End If
End Function‘以上的题目可以表述为:容积分别为a1,a2,a3的容器分别装有a,b,c数量的液体,只能通过互相倒满或者倒空一个容器,用最少步数得到指定的数量:d。’如果a1远大于b1和c1,则可以理解为a1是一个池塘或一个大酒缸。以上程序可以描述为:用9斤和7斤的量筒一次得到4斤的酒。 |
|