2012年11月20日 星期二

自動販賣機

自動販賣機的物品價格為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元
出自 程式設計隊訓練教材

2 則留言:

  1. Dim x, y, z, k, num, Money As Integer
    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, num
    Do
    For x = 0 To 1
    If x * 50 > num Then Call printt("50*" & x & "=" & x * 50 & "找" & ((x * 50) - num) & "元"): Exit For
    For y = 0 To 3
    If x * 50 + y * 10 > num Then Call printt("10*" & y & "+50*" & x & "=" & x * 50 + y * 10 & " " & "找" & ((x * 50 + y * 10) - num) & "元"): Exit For
    For z = 0 To 5
    If x * 50 + y * 10 + z * 5 > num Then Call printt("5*" & z & "+10*" & y & "+50*" & x & "=" & x * 50 + y * 10 + z * 5 & " " & "找" & ((x * 50 + y * 10 + z * 5) - num) & "元"): Exit For
    For k = 0 To 25
    Money = (x * 50) + (y * 10) + (z * 5) + (k * 1)
    If Money >= num And Money <= 50 Then Call printt("1*" & k & "+5*" & z & "+10*" & y & "+50*" & x & "=" & Money & " " & " " & "找" & (Money - num) & "元"): Exit For
    Next
    Next
    Next
    Next
    Loop Until EOF(1)
    Close #2
    Close #1
    End
    End Sub

    Public Sub printt(strr As String)
    strr = Replace(strr, "1*0+", "")
    strr = Replace(strr, "5*0+", "")
    strr = Replace(strr, "10*0+", "")
    strr = Replace(strr, "+50*0", "")
    Print #2, strr
    End Sub

    回覆刪除

  2. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    a = Split("1,5,10,50", ",")
    Input #1, n
    Print #2, "1*" & n & "=" & n & " 找0元"
    For i = 1 To 3
    r = Val(n) \ Val(a(i))
    For j = 1 To r + 1
    If Val(a(i)) * j < Val(n) Then
    ans = Val(n) - Val(a(i)) * j
    If ans Mod Val(a(i - 1)) = 0 Then Print #2, a(i - 1) & "*" & ans \ Val(a(i - 1)) & "+" & a(i) & "*" & j & "=" & Val(n) & " 找0元"
    Else
    Print #2, a(i) & "*" & j & "=" & Val(a(i)) * j & " 找" & Val(a(i)) * j - Val(n) & "元"
    End If
    Next j
    Next i
    Close #2
    Close #1
    End
    End Sub

    回覆刪除