明天就要去遠足了,小綠正打包她的行李,她家有下列五種零食:
名稱 | 大小 | 滿足感 |
海苔 | 3 | 4 |
花生米 | 4 | 5 |
點心麵 | 7 | 10 |
洋芋片 | 8 | 11 |
巧克力 | 9 | 13 |
而這五種零食一定要一整包放進背包不可以打散,現在有一個大小為 N (N<=100)的背包要用來裝零食,請問她可以獲得的最大滿足感為多少?
輸入1:17
輸出1:24
輸入2:100
輸出2:144
(網路參考:http://www.tcgs.tc.edu.tw/~sagit/cpp/q12.htm)
(網路參考:http://www.tcgs.tc.edu.tw/~sagit/cpp/q12.htm)
Dim c(5) As Integer, s(5) As Integer
回覆刪除Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, x
c(1) = 3: c(2) = 4: c(3) = 7: c(4) = 8: c(5) = 9
s(1) = 4: s(2) = 5: s(3) = 10: s(4) = 11: s(5) = 13
Print #2, abc(x, 5)
Close #2
Close #1
End Sub
Function abc(ByVal i As Integer, ByVal j As Integer) As Long
If j = 0 Then
abc = 0
Else
If i < c(j) Then
abc = abc(i, j - 1)
Else
x = abc(i - c(j), j) + s(j)
y = abc(i, j - 1)
If x > y Then
abc = x
Else
abc = y
End If
End If
End If
End Function
Private Sub Form_Load()
回覆刪除Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Dim ans(1000) As Long
Dim com(5) As Integer
Dim item(5) As Integer
item(1) = 3: item(2) = 4: item(3) = 7: item(4) = 8: item(5) = 9
com(1) = 4: com(2) = 5: com(3) = 10: com(4) = 11: com(5) = 13
Input #1, x
For j = 1 To 5
For i = 1 To x
If i >= item(j) Then
If (ans(i - item(j)) + com(j)) > ans(i) Then
ans(i) = (ans(i - item(j)) + com(j))
End If
End If
Next i
Next j
Print #2, ans(x)
Close #1
Close #2
End Sub
1234
回覆刪除