2010年2月15日 星期一

2010/02/15 自動販賣機

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

8 則留言:

  1. 這樣的題目,因為價格範圍不大,
    還是可以先暴力做做看10~25,
    做著做著,如果能找出規律,最好,如果不能,就暴力做到完吧。

    回覆刪除
  2. 老師 應該 還能 在修改一下@@
    不知道有沒有問題
    Dim co(4) As Integer
    Dim x, a, n As Integer
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, a
    co(1) = 1: co(2) = 5: co(3) = 10: co(4) = 50
    For i = 1 To 4
    x = a
    Select Case i
    Case 1
    n = x
    Print #2, co(i) & "*" & x & "="; x;
    Print #2, "找" & n - a & "元"
    Call q2
    Case 2
    n = ((a + 4) \ 5) * co(i)
    Print #2, co(i) & "*" & (a + 4) \ 5 & "="; n;
    Print #2, "找" & n - a & "元"
    Call q3
    Case 3
    n = ((a + 9) \ 10) * co(i)
    Print #2, co(i) & "*" & (a + 9) \ 10 & "="; n;
    Print #2, "找" & n - a & "元"
    Case 4
    n = 50
    Print #2, co(4) & "*" & 1 & "="; n;
    Print #2, "找" & n - a & "元"
    End Select
    Next i
    Close #1
    Close #2
    End Sub

    Public Sub q2()
    s = 1
    Do Until x < 6
    Print #2, co(1); "*"; x - 5; "+"; co(2); "*"; s; "="; a;
    Print #2, "找" & n - a & "元"
    x = x - 5
    s = s + 1
    Loop
    End Sub

    Public Sub q3()
    s = 0
    Do Until x < 11
    n5 = 1
    s = s + 1
    n = co(1) * (x - 10) + co(3) * s
    Print #2, co(1); "*"; x - 10; "+"; co(3); "*"; s; "="; n;
    Print #2, "找" & n - a & "元"
    If x > 10 Then
    If (5 < (a - 10)) And ((a - 10) <= 10) Then n5 = 2
    If a - 10 > 10 Then n5 = 3
    n = co(2) * n5 + co(3) * s
    If (n - a) > 4 Then Exit Do
    Print #2, co(2); "*"; n5; "+"; co(3); "*"; s; "="; n;
    Print #2, "找" & n - a & "元"
    End If
    x = x - 10
    Loop
    End Sub

    回覆刪除
  3. 這題我去暴力算了幾個之後,果然找到了規律。
    這題,就是三個for next 就好了。
    因為題目說10~25之間,所以
    for co50 = 0 to 1
    for co10= 0 to 3
    for co5= 0 to 5
    co1用算的。
    找多少錢,也用算的。
    試試吧。

    回覆刪除
  4. Dim x As Integer
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x
    For m10 = 0 To 3
    For m5 = 0 To x \ 5
    k = 10 * m10 + 5 * m5
    h = k - x
    If h <= 10 Then
    If k < x Then
    If m10 = 0 And m5 = 0 Then
    Print " 1*" & -h;
    Else
    Print " 1*" & -h & "+";
    End If
    check5 (m5): check10 (m10)
    Print "=" & x;
    If h > 0 Then
    Print " 找"; h; "元"
    Else
    Print " 找0元"
    End If
    Else
    check5 (m5): check10 (m10)
    Print "=" & k;
    If h > 0 Then
    Print " 找"; h; "元"
    Else
    Print " 找0元"
    End If
    End If
    End If
    Next m5, m10
    Print " 50*1 = 50 找" & 50 - x & "元"
    Close #1
    Close #2
    End Sub
    Public Function check5(a)
    If a > 0 Then
    Print " 5*" & a & "+";
    End If
    End Function
    Public Function check10(a)
    If a > 0 Then
    Print " 10*" & a;
    End If
    End Function

    回覆刪除
  5. 阿瑋好,
    應該是對了。
    只是那三個if連在一起,還是有改進空間的。
    以為會很簡單的,還是累人了些。(還是這幾天在忙別的事,有些煩?sorry).

    回覆刪除
  6. 完成了@@
    蠻多IF的 不判斷不行@@
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, a
    mon = a
    For cos10 = 0 To 3
    If cos10 = 0 Then
    For cos5 = 0 To 5
    If (mon - cos5 * 5) > -5 Then
    If cos5 = 0 Then
    Print #2, mon - cos5 * 5; "個1元"
    Else
    If mon - cos5 * 5 > 0 Then
    Print #2, cos5; "個5元"; mon - cos5 * 5; "個1元";
    Print #2, " 找0元"
    Else
    Print #2, cos5; "個5元";
    Print #2, " 找"; cos5 * 5 - mon; "元"
    End If
    End If
    End If
    Next cos5
    Else

    For cos5 = 0 To 5
    If (mon - cos10 * 10 - cos5 * 5) > -5 Then
    Print #2, cos10; "個10元";

    If cos5 = 0 Then
    If (mon - cos10 * 10) > 0 Then Print #2, (mon - cos10 * 10); "個1元";
    Print #2, " 找0元"
    Else
    If (mon - cos10 * 10 - cos5 * 5) > 0 Then
    Print #2, cos5; "個5元"; (mon - cos10 * 10 - cos5 * 5); "個1元";
    Print #2, " 找0元"
    Else
    If (mon - cos10 * 10 - cos5 * 5) > -5 Then
    Print #2, cos5; "個5元";
    Print #2, " 找"; cos10 * 10 + cos5 * 5 - mon; "元"
    Else
    Print #2,
    End If
    End If

    End If

    End If

    Next cos5
    mon = a
    If (mon - cos10 * 10) < 0 And (mon - cos10 * 10) > -10 Then
    Print #2, cos10; "個10元";
    Print #2, "找"; cos10 * 10 - mon; "元"
    End If
    End If

    Next cos10
    Print #2, 1; "個50元 找"; 50 - a; "元 "
    Close #1
    Close #2
    End Sub


    輸入25
    輸出
    25 個1元
    1 個5元 20 個1元 找0元
    2 個5元 15 個1元 找0元
    3 個5元 10 個1元 找0元
    4 個5元 5 個1元 找0元
    5 個5元 找 0 元
    1 個10元 15 個1元 找0元
    1 個10元 1 個5元 10 個1元 找0元
    1 個10元 2 個5元 5 個1元 找0元
    1 個10元 3 個5元 找 0 元
    2 個10元 5 個1元 找0元
    2 個10元 1 個5元 找 0 元
    3 個10元找 5 元
    1 個50元 找 25 元

    回覆刪除
  7. Private Sub Form_Load()
    Open App.Path & "/in.txt" For Input As #1
    Input #1, q
    Close #1
    Open App.Path & "/out.txt" For Output As #2
    Print #2, "50*1=50" & ",找" & Abs(50 - q) & "元"
    For co10 = 0 To 3
    If q - 10 * co10 <= -10 Then GoTo NextOne
    For co5 = 0 To 5
    If co5 > 0 And q - 5 * co5 - 10 * co10 <= -5 Then GoTo NextOne
    co1 = 0
    If q - co5 * 5 - co10 * 10 > 0 Then co1 = q - co5 * 5 - co10 * 10: Print #2, "1*" & q - co5 * 5 - co10 * 10;
    If co5 > 0 Then
    If co1 > 0 Then
    Print #2, "+5*" & co5;
    Else
    Print #2, "5*" & co5;
    End If
    End If
    If co10 > 0 Then
    If co1 > 0 Or co5 > 0 Then
    Print #2, "+10*" & co10;
    Else
    Print #2, "10*" & co10;
    End If
    End If
    Print #2, "=" & co1 + co5 * 5 + co10 * 10 & ",找" & Abs(q - co1 - co5 * 5 - co10 * 10) & "元"
    Next co5
    NextOne:
    Next co10
    Close #2
    End Sub

    回覆刪除
  8. 高仔好,
    你的程式應該ok,這題看得都累了。(還是我今天有些累了呢?)
    星期一,再來檢討看看,還是由我來找題目呢,怎麼覺得選得這些題目,我的興趣不多呢。
    討論討論。

    回覆刪除