2012年11月20日 星期二

購買花朵


鬱金香一朵50元、香水百合一朵10元、白玫瑰一朵5元、滿天星一朵1元,現王先生有一筆金額N (0<100),請設計一程式,計算出此金額若全部用完,能買到的花朵數。(買到的花朵數必須為最少,花朵數若非最少,則本題算錯,零分計算。)

輸入說明:輸入金額n
輸入範例:假定先生有 78元。實際輸入之檔案內容如下:
實際輸入:78

輸出說明:第一列為花朵總數。第二列到第五列分別為鬱金香、香水百合、白玫瑰、滿天星的花朵數。
輸出範例:上例中先生有 78元,能買到最少的花朵數分別為鬱金香1朵、香水百合2朵白、玫瑰1朵、滿天星3朵。實際輸出之檔案內容如下:
實際輸出:
                7
                1
                2
                1
                3

4 則留言:

  1. Dim num, total, first, second, third, forth 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
    first = num \ 50
    num = num Mod 50
    second = num \ 10
    num = num Mod 10
    third = num \ 5
    num = num Mod 5
    forth = num \ 1
    num = num Mod 1
    total = first + second + third + forth
    Print #2, total
    Print #2, first
    Print #2, second
    Print #2, third
    Print #2, forth
    Loop Until num = 0
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  2. Dim a(4)
    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, x
    a(1) = x \ 50: x = x Mod 50
    a(2) = x \ 10: x = x Mod 10
    a(3) = x \ 5: x = x Mod 5
    a(4) = x
    Print #2, a(1) + a(2) + a(3) + a(4)
    For i = 1 To 4
    Print #2, a(i)
    Next
    Close
    Close
    End
    End Sub

    回覆刪除
  3. Dim z, x, c, v 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, n
    Do
    Select Case n
    Case Is > 50
    n = n - 50
    z = z + 1
    Case Is < 50 And n > 10
    n = n - 10
    x = x + 1
    Case Is < 10 And n > 5
    n = n - 5
    c = c + 1
    Case Is < 5
    n = n - 1
    v = v + 1
    End Select
    Loop Until n = 0
    Print #2, z + x + c + v
    Print #2, z
    Print #2, x
    Print #2, c
    Print #2, v
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  4. 以上程式正確:)

    復陞題目是階層式的減少,所以可以不用選擇結構。

    回覆刪除