子題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
Dim money, listt1, listt2, mycount, okstr(99), mycountt
回覆刪除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, mycount
Line Input #1, money
money = Split(money, ", ")
listt1 = Split("肉 菜 蛋 果", " ")
listt2 = Split("(肉) (菜) (蛋) (果)", " ")
Call rr("", 1)
For i = 0 To List1.ListCount - 1
For ii = 1 To mycountt
If Val(Right(okstr(ii), 3)) = Val(List1.List(i)) + 100000 Then Print #2, okstr(ii)
Next
Next
Close #2
Close #1
End
End Sub
Public Function rr(ByVal k As String, ByVal x As Integer)
If Len(k) = mycount Then
mycountt = mycountt + 1
tempp = "肉菜蛋果"
For ai = 1 To 2
tempp = Replace(tempp, listt1(Val(Mid(k, ai, 1)) - 1), listt2(Val(Mid(k, ai, 1)) - 1))
moneyy = Val(money(Val(Mid(k, ai, 1)) - 1)) + moneyy
Next
List1.AddItem moneyy - 100000
okstr(mycountt) = tempp & " " & moneyy: moneyy = 0
Else
For i = x To 4
If InStr(k, i) = 0 Then Call rr(k & i, i)
Next
End If
End Function
Dim ve()
回覆刪除Dim kg() As String
Dim namer() As String
Dim mcount, n, money(), r 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
n = Val(n)
Line Input #1, y
ve() = Array("肉", "菜", "蛋", "果")
kg = Split(y, ",")
r = 1
Call a("", 0, 0)
For i = 1 To UBound(money)
For j = i + 1 To UBound(namer)
If money(i) < money(j) Then xc = money(i): money(i) = money(j): money(j) = xc: xv = namer(i): namer(i) = namer(j): namer(j) = xv
Next j
Next i
For i = 1 To UBound(money)
Print #2, namer(i) & " " & money(i)
Next i
Close #2
Close #1
End
End Sub
Function a(b, c, mm)
If Len(b) = n Then
For i = 1 To n
For j = 0 To UBound(ve)
If j = Val(Mid(b, i, 1)) Then ve(j) = "(" & ve(j) & ")"
Next j
Next i
For i = 0 To UBound(ve)
strB = strB & ve(i)
Next i
ReDim Preserve money(r), namer(r)
money(r) = mm: namer(r) = strB
ve() = Array("肉", "菜", "蛋", "果"): r = r + 1
Else
For i = c To UBound(ve)
If InStr(b, i) = 0 Then Call a(b & i, i, mm + kg(i))
Next
End If
End Function