Testing Fractional Knapsack Algorithm Implementation in hello-algo
This test suite validates the fractional knapsack algorithm implementation in Go, examining how items with different weights and values are optimally selected to maximize value within capacity constraints. The test verifies the greedy approach for solving the fractional knapsack problem.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
krahets/hello-algo
codes/go/chapter_greedy/fractional_knapsack_test.go
// File: fractional_knapsack_test.go
// Created Time: 2023-07-23
// Author: Reanon ([email protected])
package chapter_greedy
import (
"fmt"
"testing"
)
func TestFractionalKnapsack(t *testing.T) {
wgt := []int{10, 20, 30, 40, 50}
val := []int{50, 120, 150, 210, 240}
capacity := 50
// 贪心算法
res := fractionalKnapsack(wgt, val, capacity)
fmt.Println("不超过背包容量的最大物品价值为", res)
}