2012年8月24日 星期五

99模擬 Problem 4-2


子題2(11%):如果有一個客人來買這個菜攤的n 項商品,每類商品只能買1 項或不買,請
選手列出所有可能的購買組合。輸出之順序,應依照購買之總金額由高而低依序輸出。
輸入說明:
第1 行有1 個數字,代表n 的值。
第2 行有4 組數字,以逗號隔開,分別表示「肉」、「菜」、「蛋」、「果」每項商品的購買金額。
輸出說明:
每列輸出一組購買組合及其購買總金額,並至少以1 個空白隔開。其輸出方式為:每列均輸
出「肉菜蛋果」字串,但客人「購買」之商品名稱以「小括號」括起來。輸出之順序依購買
之總金額由高而低依序輸出,若有總金額相同者則不限順序。
輸入範例:【檔名:in-4-2.txt】
2
140, 64, 36, 84
輸出範例:【檔名:out-4-2.txt】
(肉)菜蛋(果) 224
(肉)(菜)蛋果 204
(肉)菜(蛋)果 176
肉(菜)蛋(果) 148
肉菜(蛋)(果) 120
肉(菜)(蛋)果 100

1 則留言:

  1. Dim a, b, c
    Dim e 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
    Line Input #1, x
    x = Replace(x, ",", "")
    a = Split(x, " ")
    b = Split("肉 菜 蛋 果", " ")
    c = Split("(肉) (菜) (蛋) (果)", " ")
    f = "肉菜蛋果"
    Call abc("")
    For i = 0 To (List1.ListCount - 1)
    ans = ""
    ans1 = ""
    For j = 1 To n
    d = Mid(List1.List(i), j, 1)
    ans = ans & "(" & d & ")"
    ans1 = "(" & d & ")" & ans1
    Next
    e = True
    For k = 0 To List2.ListCount
    If ans = List2.List(k) Then e = False: Exit For
    If ans1 = List2.List(k) Then e = False: Exit For
    Next
    If e = True Then List2.AddItem ans
    Next
    For i = 0 To (List2.ListCount - 1)
    ans2 = ""
    ans3 = 0
    For j = 1 To 4
    If Mid(f, j, 1) = Mid(List2.List(i), 2, 1) Then
    ans2 = ans2 & Mid(List2.List(i), 1, 3): ans3 = ans3 + a(j - 1)
    ElseIf Mid(f, j, 1) = Mid(List2.List(i), 5, 1) Then
    ans2 = ans2 & Mid(List2.List(i), 4, 3): ans3 = ans3 + a(j - 1)
    ElseIf Mid(f, j, 1) = Mid(List2.List(i), 8, 1) Then
    ans2 = ans2 & Mid(List2.List(i), 7, 3): ans3 = ans3 + a(j - 1)
    ElseIf Mid(f, j, 1) = Mid(List2.List(i), 11, 1) Then
    ans2 = ans2 & Mid(List2.List(i), 10, 3): ans3 = ans3 + a(j - 1)
    Else
    ans2 = ans2 & Mid(f, j, 1)
    End If
    Next
    e = True
    For k = 0 To List3.ListCount
    If (ans3 + 10000) & " " & ans2 = List3.List(k) Then e = False: Exit For
    Next
    If e = True Then List3.AddItem (ans3 + 10000) & " " & ans2
    Next

    For i = (List3.ListCount - 1) To 0 Step -1
    g = Split(List3.List(i))
    Print #2, g(1) & " " & (g(0) - 10000)
    Next
    Close
    Close
    End
    End Sub
    Sub abc(t)
    If Len(t) = 4 Then
    List1.AddItem t
    Else
    For i = 0 To 3
    If InStr(t, b(i)) = 0 Then Call abc(t & b(i))
    Next
    End If
    End Sub

    回覆刪除