2012年9月13日 星期四

自動賣票機找零問題

某城市的捷運車票共有20、25、30、35、40、45六種,而可投入的錢幣共有1、5、10、50、100元五種。
請計算可能的投幣方式與需找回的零錢金額。
(當所投入的金額超過票價後,多投的錢幣會直接吐還給使用者,所以,多投的部分不用計算。例如,票價20元,當依序投入7個1元、3個5元後,再來投入任何錢幣都會直接先吐還使用者。直接先吐還的這部分,不用寫入程式。)
輸入:in.txt
20
輸出:out.txt
投入20個1元,找回0元。
投入15個1元 1個5元,找回0元。
投入10個1元 2個5元,找回0元。
投入5個1元 3個5元,找回0元。

投入1個50元,找回30元。
投入1個100元,找回80元。

1 則留言:

  1. Dim n As Integer
    Dim a(3) As Integer
    Dim h As Boolean
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n
    For co1 = 0 To 45
    For co5 = 0 To 9
    For co10 = 0 To 5
    If co1 = n And co5 = 0 And co10 = 0 Then a(1) = co1
    If co1 + co5 * 5 = n And co10 = 0 Then a(1) = co1: a(2) = co5
    If co1 + co5 * 5 + co10 * 10 = n Then a(1) = co1: a(2) = co5: a(3) = co10
    If co5 * 5 + co10 * 10 = n And co1 = 0 Then a(2) = co5: a(3) = co10
    If co1 + co10 * 10 = n And co5 = 0 Then a(1) = co1: a(3) = co10
    If co1 = 0 And co5 * 5 = n And co10 = 0 Then a(2) = co5
    If co1 = 0 And co5 * 5 = 0 And co10 * 10 = n Then a(3) = co10
    Call abc(a(1), a(2), a(3))
    Next
    Next
    Next
    For i = 1 To (List1.ListCount - 1)
    ans = ""
    k = Split(List1.List(i), " ")
    d = k(0)
    b = k(1)
    c = k(2)
    If d <> 0 Then ans = "投入" & d & "個1元,"
    If b <> 0 Then ans = ans & "投入" & b & "個5元,"
    If c <> 0 Then ans = ans & " " & "投入" & c & "個10元,"
    e = d + b * 5 + c * 10 - n
    ans = ans & "找回" & e & "元。"
    If e >= 0 Then Print #2, ans
    Next
    Print #2, "投入1個50元,找回" & 50 - n & "元。"
    Print #2, "投入1個100元,找回" & 100 - n & "元。"
    Close
    Close
    End
    End Sub
    Sub abc(d, b, c)
    ans = ""
    h = True
    f = d & " " & b & " " & c
    For i = 0 To List1.ListCount
    If f = List1.List(i) Then h = False: Exit For
    Next
    If h = True Then List1.AddItem f
    For i = 1 To 3
    a(i) = 0
    Next
    End Sub

    回覆刪除