AcWing 3. 完全背包问题-二维数组-golang
原题链接
简单
作者:
一只鱼
,
2021-03-19 10:51:27
,
所有人可见
,
阅读 310
完全背包问题-二维数组-golang
golang
package main
import "fmt"
var n, v int //种类,总体积
var vi, wi []int //体积, 价值
var value [][]int //种类,体积
func main(){
fmt.Scanf("%d%d", &n, &v)
vi, wi = make([]int, n+1), make([]int, n+1)
value = make([][]int, n+1)
for i := 0; i <= n; i++{
value[i] = make([]int, v+1)
}
for i := 1; i <= n; i++{
fmt.Scanf("%d%d", &vi[i], &wi[i])
}
for i := 1; i <= n; i++{
for j := 1; j <= v; j++{
for k := 0; k*vi[i] <= j; k++{
value[i][j] = max(value[i][j], value[i-1][j-k*vi[i]]+k*wi[i])
}
}
}
fmt.Println(value[n][v])
}
func max(a, b int)int{
if a >b{
return a
}
return b
}