2012年9月10日 星期一

自動販賣機

自動販賣機的物品價格為10-25元,銅板種類1,5,10,50四種,讀取"in.txt"物品價格,計算可能的投幣方式與需找回的零錢。
輸入範例:

11
輸出範例: 

1*11=11 找0元
1*6+5*1=11 找0元
1*1+5*2=11 找0元
5*3=15 找4元
1*1+10*1=11 找0元
5*1+10*1=15 找4元
10*2=20 找9元
50*1=50 找39元
出自 程式設計隊訓練教材

1 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Dim a As Boolean
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n
    For co50 = 0 To 1
    For co10 = 1 To 3
    For co5 = 1 To 5
    For co1 = 1 To 25
    If co1 * 1 = n Then List1.AddItem "1 *" & co1 & "=" & co1 * 1 & "找0元"
    If co50 * 50 > n Then List1.AddItem "50 *" & co50 & "=" & co50 * 50 & "找回 " & co50 * 50 - n & "元"
    If co10 * 10 >= n And co10 * 10 <= n + 9 Then List1.AddItem "10 *" & co10 & "=" & co10 * 10 & "找回" & co10 * 10 - n & "元"
    If co5 * 5 >= n And co5 * 5 < n + 9 Then List1.AddItem "5 *" & co5 & "=" & co5 * 5 & "找回" & co5 * 5 - n & "元"
    If co10 * 10 + co5 * 5 + co1 * 1 >= n And co10 * 10 + co5 * 5 + co1 * 1 < n + 1 Then List1.AddItem "10 *" & co10 & "+ 5 * " & co5 & "+ 1 * " & co1 & "=" & co10 * 10 + co5 * 5 + co1 * 1 & "找回" & co10 * 10 + co5 * 5 + co1 * 1 - n & "元"
    If co10 * 10 + co5 * 5 >= n And co10 * 10 + co5 * 5 <= n + 4 Then List1.AddItem "10 *" & co10 & "+ 5 * " & co5 & "=" & co10 * 10 + co5 * 5 & "找回" & co10 * 10 + co5 * 5 - n & "元"
    If co10 * 10 + co1 * 1 >= n And co10 * 10 + co1 * 1 < n + 1 Then List1.AddItem "10 *" & co10 & "+ 1 * " & co1 & "=" & co10 * 10 + co1 * 1 & "找回" & co10 * 10 + co1 * 1 - n & "元"
    If co5 * 5 + co1 * 1 >= n And co5 * 5 + co1 * 1 < n + 1 Then List1.AddItem "5 *" & co5 & "+ 1 * " & co1 & "=" & co5 * 5 + co1 * 1 & "找回" & co5 * 5 + co1 * 1 - n & "元"
    Next
    Next
    Next
    Next
    For k = 1 To 50
    For i = 0 To List1.ListCount
    For j = i + 1 To List1.ListCount
    If List1.List(i) = List1.List(j) Then List1.RemoveItem (i)
    Next
    Next
    Next
    For i = 0 To List1.ListCount
    Print #2, List1.List(i)
    Next
    Close
    Close
    End
    End Sub

    回覆刪除