题目
经典完全背包问题,用golang写一遍
代码
package main
import "fmt"
func main() {
var n, m int
fmt.Scan(&n, &m)
v := make([]int, n+1)
w := make([]int, n+1)
for i := 1; i <= n; i++ {
fmt.Scan(&v[i], &w[i])
}
dp := make([][]int, n+1)
for i := range dp {
dp[i] = make([]int, m+1)
}
for i := 1; i <= n; i++ {
for j := 0; j <= m; j++ {
for k := 0; k*v[i] <= j; k++ {
dp[i][j] = max(dp[i][j], dp[i-1][j-k*v[i]]+k*w[i])
}
}
}
fmt.Println(dp[n][m])
}
func max(a, b int) int {
if a > b {
return a
}
return b
}