有一天菜攤上有 8種類型商品,包括「肉」、「菜」、「蛋」、「果」、「魚」、「蝦」、「豆」及「菇」,每種產品單項價格由輸入檔輸入。某個前來購買的客人,共有 k元預算,表示客人的總購買金額不可超過 k元。若客人對於每種類型商品只可選擇買一項或不買,在預算之內,客人共有那些購買組合?
輸入說明:第 1行是客人之預算金額 k,其值為整數,並且不超過 5000元。第 2~9行有各商品的品名及單價,中間以逗號隔開。商品單價均為整數,且不超過 200元。
輸出說明:每行輸出一種符合預算的購買組合及其支出總金額,但不輸出「都不買」的組合。輸出資料(購買的商品組合及其支出總金額)間,以至少 1個空白隔開。若購買組合包括多種商品,則商品間不限制是否有空白相隔。選手請依支出總金額由大到小依序輸出,但支出金額相同之資料,其輸出順序不限。
輸入檔案 1:【檔名:in1.txt】90
肉, 65
菜, 45
蛋, 30
果, 75
魚, 80
蝦, 95
豆, 55
菇, 60
輸入檔案 2:【檔名:in2.txt】
100
肉, 65
菜, 45
蛋, 30
果, 75
魚, 80
蝦, 95
豆, 55
菇, 60
蛋菇 90
蛋豆 85
魚 80
果 75
菜蛋 75
肉 65
菇 60
豆 55
菜 45
蛋 30
菜豆 100
蝦 95
肉蛋 95
蛋菇 90
蛋豆 85
魚 80
菜蛋 75
果 75
肉 65
菇 60
豆 55
菜 45
蛋 30
Dim namee(8), k, okstr(99) As String
回覆刪除Dim cost(8), i, bi, p, Money, mycount As Integer
Private Sub Form_Load()
Me.Hide
Open App.Path & "\out.txt" For Output As #1
Close #1
Open App.Path & "\in1.txt" For Input As #1
Open App.Path & "\in2.txt" For Input As #2
Open App.Path & "\out.txt" For Append As #3
For ki = 1 To 2
Input #ki, Money
For ii = 1 To 8
Input #ki, namee(ii), cost(ii)
Next
For bi = 0 To 7
Call rr("", 1, bi)
Next
For ci = 0 To List1.ListCount - 1
For di = 1 To mycount
If Val(List1.List(ci)) + 100000 = Val(Mid(okstr(di), (InStr(okstr(di), " ") + 1), Len(okstr(di)) - InStr(okstr(di), " "))) Then Print #3, okstr(di)
Next
Next
Print #3,
mycount = 0: List1.Clear
Next
Close #3
Close #2
Close #1
End
End Sub
Public Function rr(k As String, ByVal x As Integer, ByVal p As Integer)
If Len(k) > p Then
For i = 1 To 8
For ii = 1 To Len(k)
If Mid(k, ii, 1) = namee(i) Then moneyy = moneyy + cost(i)
Next
Next
If Val(moneyy) <= Val(Money) Then mycount = mycount + 1: okstr(mycount) = k & " " & moneyy: List1.AddItem moneyy - 100000: moneyy = 0
Else
For x = x To 8
If InStr(k, namee(x)) = 0 Then Call rr(k & namee(x), x + 1, bi)
Next
End If
End Function
Dim a(), c() As String
回覆刪除Dim b(), d(), g(2) As Integer
Private Sub Form_Load()
Me.Hide
Open App.Path & "\in1.txt" For Input As #1
Open App.Path & "\in2.txt" For Input As #2
Open App.Path & "\out.txt" For Output As #3
Input #1, money: Input #2, mon
g(1) = Val(money): g(2) = Val(mon)
For w = 1 To 2
r = 1
Do While Not EOF(w)
Line Input #w, x
ReDim Preserve a(r), b(r)
a(r) = Left(x, 1): b(r) = Val(Right(x, Len(x) - 2)): r = r + 1
Loop
s = 1
For i = 1 To UBound(b)
If b(i) <= g(w) Then
ReDim Preserve c(s), d(s)
c(s) = a(i): d(s) = b(i): s = s + 1
End If
For j = i + 1 To UBound(b)
If b(i) + b(j) <= g(w) Then
ReDim Preserve c(s), d(s)
c(s) = a(i) & a(j): d(s) = b(i) + b(j)
s = s + 1
End If
Next j
Next i
For i = 1 To UBound(d)
For j = i + 1 To UBound(d)
If d(i) < d(j) Then xc = d(i): d(i) = d(j): d(j) = xc: xv = c(i): c(i) = c(j): c(j) = xv
Next j
Next i
For i = 1 To UBound(d)
Print #3, c(i) & " " & d(i)
Next i
Print #3,
Next w
Close #3
Close #2
Close #1
End
End Sub