You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
741 B

1 year ago
package main
import "golang.org/x/exp/constraints"
type Book struct {
Weight uint
Value uint
}
func MaxValue(capacity uint, books []Book) uint {
if capacity == 0 {
return 0
}
if len(books) == 0 {
return 0
}
book := books[0]
return Max(
book.Value+MaxValue(capacity-book.Weight, books[1:]),
MaxValue(capacity, books[1:]),
)
}
func main() {
println("Did it")
}
func Max[T constraints.Ordered](args ...T) T {
if len(args) == 0 {
return *new(T) // zero value of T
}
if isNan(args[0]) {
return args[0]
}
max := args[0]
for _, arg := range args[1:] {
if isNan(arg) {
return arg
}
if arg > max {
max = arg
}
}
return max
}
func isNan[T constraints.Ordered](arg T) bool {
return arg != arg
}