2011年10月19日 星期三

97模擬 Problem 2 (惱人的零錢

東東在買東西付帳,總是習慣直接從錢包中拿鈔票付帳,而懶得掏出硬幣來。久而久之,錢包裡面累積了許多硬幣,簡直重得不得了,所以東東終於受夠了!因此,她決定趁著今天買東西的時候,想辦法盡量減輕負擔。於是東東開始盤算要怎樣湊出足夠的硬幣,才能讓付出去的硬幣個數越多越好。同時,目前這家店的老闆人很好,不論客人給他多少硬幣,他都一定會用最少的硬幣找錢給客人。所以,當東東走到櫃台結帳時,東東想到自己如果多付一些硬幣讓老闆找錢,說不定可以讓自己的錢包更輕!因此,東東開始煩惱到底要怎麼給錢,才能夠盡量「用掉」最多的硬幣呢(所謂的「用掉」的硬幣個數,指的是拿出去的硬幣數,扣掉老闆找回來的硬幣數)?可惜的是,東東的算術一向不太靈光,因此希望你能幫忙他解決這個煩惱。

輸入說明:
  輸入資料的第一行是一個整數n,代表共有n 筆測試資料。接下來每筆測試資料有3 行:第1 行的數字C 表示要買的東西的價格。第2 行有5 個數字p1 p5 p10 p20 p50,分別是東東錢包裡面一元、五元、十元、二十元和五十元硬幣的個數。第3 行有5 個數字q1 q5 q10 q20 q50,是老闆所擁有的一元、五元、十元、二十元和五十元硬幣的個數。每筆測試資料的所有數字都在0 10000 之間;同一行的數字之間會用一個空白隔開。你可以假設東東身上的錢足夠來購買該商品,而且至少有一種付錢的方法使得老闆可以找得開(如果需要找錢的話)。因為老闆和東東很不幸地很碰巧地一張鈔票都沒有,請不要問說為什麼不能換成大鈔。

輸出說明:
  你的輸出資料應該有n 行,分別對應到n 筆輸入的測試資料。每一行要輸出一個數字表示東東付完帳之後,剩餘的硬幣總數。

輸入範例:
2
25
10 3 2 1 3
0 0 0 0 0
25
0 3 2 2 3
1 1 1 1 1

輸出範例:
6
4

6 則留言:

  1. Private Sub Form_Load()
    Dim x(5)
    Dim y(5)
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, N
    For i = 1 To N
    Input #1, pay
    For j = 1 To 5: Input #1, x(j): Next j
    For j = 1 To 5: Input #1, y(j): y(j) = y(j) + x(j): Next j
    Ctotal = x(1) + x(2) * 5 + x(3) * 10 + x(4) * 20 + x(5) * 50
    back = Ctotal - pay
    ans = 0

    If back >= 50 And y(5) >= 1 Then
    Do
    back = back - 50
    y(5) = y(5) - 1
    ans = ans + 1
    Loop Until back < 50 Or y(5) = 0
    End If

    If back >= 20 And y(4) >= 1 Then
    Do
    back = back - 20
    y(4) = y(4) - 1
    ans = ans + 1
    Loop Until back < 20 Or y(4) = 0
    End If

    If back >= 10 And y(3) >= 1 Then
    Do
    back = back - 10
    y(3) = y(3) - 1
    ans = ans + 1
    Loop Until back < 10 Or y(3) = 0
    End If

    If back >= 5 And y(2) >= 1 Then
    Do
    back = back - 5
    y(2) = y(2) - 1
    ans = ans + 1
    Loop Until back < 5 Or y(2) = 0
    End If

    If back >= 1 And y(1) >= 1 Then
    Do
    back = back - 1
    y(1) = y(1) - 1
    ans = ans + 1
    Loop Until back = 0 Or y(1) = 0
    End If

    Print #2, ans

    Next i
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  2. Dim s, ss

    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, t
    For i = 1 To t
    Input #1, N
    Line Input #1, W
    Line Input #1, L
    Call A1(N, W, L)
    Next
    Close
    Close
    End
    End Sub


    Sub A1(a, b, c)

    Dim sum
    s = Split(b)
    sp = Split(b)
    ss = Split(c)
    sk = Split(b)
    For i = 0 To UBound(s): sk(i) = 0: Next
    For i = 0 To UBound(s): sum = sum + Val(s(i)): Next

    ' 1$
    For i = 1 To s(0)
    If a - 1 >= 0 Then a = a - 1: sum = sum - 1: sp(0) = sp(0) - 1
    Next

    '5$
    For i = 1 To s(1)
    If a - 5 >= 0 Then a = a - 5: sum = sum - 1: sp(1) = sp(1) - 1
    Next

    '10$
    For i = 1 To s(2)
    If a - 10 >= 0 Then a = a - 10: sum = sum - 1: sp(2) = sp(2) - 1
    Next

    '20$
    For i = 1 To s(3)
    If a - 20 >= 0 Then a = a - 20: sum = sum - 1: sp(3) = sp(3) - 1
    Next

    '50$
    For i = 1 To s(4)
    If a - 50 >= 0 Then a = a - 50: sum = sum - 1: sp(4) = sp(4) - 1
    Next



    ' Chang Money

    AllMoney = sp(0) + sp(1) * 5 + sp(2) * 10 + sp(3) * 20 + sp(4) * 50
    For i = 0 To UBound(ss)
    ss(i) = ss(i) + sp(i)
    Next
    AllKind = sum

    For i = 1 To ss(4)
    If AllMoney - 50 >= 0 Then AllMoney = AllMoney - 50: sk(4) = sk(4) + 1
    Next
    For i = 1 To ss(3)
    If AllMoney - 20 >= 0 Then AllMoney = AllMoney - 20: sk(3) = sk(3) + 1
    Next
    For i = 1 To ss(2)
    If AllMoney - 10 >= 0 Then AllMoney = AllMoney - 10: sk(2) = sk(2) + 1
    Next
    For i = 1 To ss(1)
    If AllMoney - 5 >= 0 Then AllMoney = AllMoney - 5: sk(1) = sk(1) + 1
    Next
    For i = 1 To ss(0)
    If AllMoney - 1 >= 0 Then AllMoney = AllMoney - 1: sk(0) = sk(0) + 1
    Next

    AllsKind = Val(sk(0)) + Val(sk(1)) + Val(sk(2)) + Val(sk(3)) + Val(sk(4))
    If AllKind > AllsKind Then Print #2, AllsKind Else Print #2, AllKind

    End Sub

    回覆刪除
  3. 是否大大能寫2010的語言= =
    雖然知道演算法是一致
    但是我看了有點霧煞煞
    是我離VB6太遠了= = 還是我是菜鳥的緣故?

    回覆刪除
  4. 作者已經移除這則留言。

    回覆刪除
  5. Public Class Form1
    'C() 客人身上的硬幣數 B() 老闆與客人總共的硬幣數
    Dim ins, outs, line(), C(), B() As String
    'P 支付 R 剩餘在身上的硬幣數 A 扣除支付後的錢
    Dim n, P, R, A As Integer
    '幣值
    Dim co() As Integer = {1, 5, 10, 20, 50}
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ins = My.Computer.FileSystem.ReadAllText("test2.txt")
    line = Split(ins, vbNewLine)
    n = line(0)
    For i = 1 To UBound(line)
    P = line(i) : i += 1
    C = Split(line(i), " ") : i += 1
    B = Split(line(i), " ")
    For j = 0 To 4
    A += Val(C(j)) * co(j)
    B(j) = Val(B(j)) + Val(C(j))
    Next
    A -= P
    For j = 4 To 0 Step -1
    Do Until A < co(j) Or B(j) = 0
    Call count(j)
    Loop
    Next
    outs &= R & vbNewLine
    A = 0 : R = 0
    Next
    My.Computer.FileSystem.WriteAllText("result2.txt", outs, False)
    End
    End Sub
    Function count(ByVal j As Integer)
    A -= co(j) : B(j) -= 1 : R += 1
    End Function
    End Class

    我用VB NET 寫的 :)

    回覆刪除