Private Sub Form_Load() Me.Hide Open App.Path & "\in.txt" For Input As #1 Open App.Path & "\out.txt" For Output As #2
Do While Not EOF(1) Input #1, X Call ABC(X, 0, 0, 0, 0) Print #2, List1.ListCount List1.Clear Loop Close #2 Close #1 End End Sub
Sub ABC(ByVal A, ByVal M1, ByVal M5, ByVal M10, ByVal M50) If A = 0 Then ans = M1 & " " & M5 & " " & M10 & " " & M50 re = False For i = 0 To (List1.ListCount - 1) If ans = List1.List(i) Then re = True: Exit For Next i If re = False Then List1.AddItem ans Else
If A >= 50 Then Call ABC((A - 50), M1, M5, M10, M50 + 1) If A >= 10 Then Call ABC((A - 10), M1, M5, M10 + 1, M50) If A >= 5 Then Call ABC((A - 5), M1, M5 + 1, M10, M50) If A >= 1 Then Call ABC((A - 1), M1 + 1, M5, M10, M50)
這樣應該是對的,
回覆刪除可是跑很久,
我還有加入判斷有無重複...
Dim X As Integer
Dim M(4) As Byte
Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Do While Not EOF(1)
Input #1, X
Call ABC(X, 0, 0, 0, 0)
Print #2, List1.ListCount
List1.Clear
Loop
Close #2
Close #1
End
End Sub
Sub ABC(ByVal A, ByVal M1, ByVal M5, ByVal M10, ByVal M50)
If A = 0 Then
ans = M1 & " " & M5 & " " & M10 & " " & M50
re = False
For i = 0 To (List1.ListCount - 1)
If ans = List1.List(i) Then re = True: Exit For
Next i
If re = False Then List1.AddItem ans
Else
If A >= 50 Then Call ABC((A - 50), M1, M5, M10, M50 + 1)
If A >= 10 Then Call ABC((A - 10), M1, M5, M10 + 1, M50)
If A >= 5 Then Call ABC((A - 5), M1, M5 + 1, M10, M50)
If A >= 1 Then Call ABC((A - 1), M1 + 1, M5, M10, M50)
End If
End Sub
佑好,
回覆刪除程式正確。
但是你說的「跑很久」這點不好,這屆的教授很在意這玩意兒。
要改進你的程式的話,建議將1元硬幣直接算,而不要放在遞迴裡。
(這點,arro的程式常出現,也要改進。)
熊掌好,
回覆刪除是把剩下的摳摳全部給1元摟!!?
佑好,
回覆刪除是的。