feat(Go-Tool):修改单测位置,添加lomuto查找第k小元素算法代码及其单测(分治法)

This commit is contained in:
huangzj
2020-08-18 17:29:42 +08:00
parent 3f0e3b27f7
commit fa58e1a97f
27 changed files with 322 additions and 251 deletions
@@ -0,0 +1,50 @@
/*
* @Author : huangzj
* @Time : 2020/7/31 15:35
* @Description:背包最优问题工具类
* 基本场景:给定n个重量为w1w 1w2w 2 ,w3w 3 ,…,wnw n ,价值为v1v 1 ,v2v 2 ,v3v 3 ,…,vnv n 的物品和容量为CC的背包,求这个物品中一个最有价值的子集,使得在满足背包的容量的前提下,包内的总价值最大
* 参考地址:https://blog.csdn.net/chanmufeng/article/details/82955730
*/
package knapsackOptimization
import (
"fmt"
"testing"
)
func TestKnapsackOptimizationUtil(t *testing.T) {
bagItem := make([]*BagItem, 0)
bagItem = append(bagItem, &BagItem{
Value: 3,
Weight: 2,
})
bagItem = append(bagItem, &BagItem{
Value: 3,
Weight: 3,
})
bagItem = append(bagItem, &BagItem{
Value: 4,
Weight: 4,
})
bagItem = append(bagItem, &BagItem{
Value: 5,
Weight: 5,
})
bagItem = append(bagItem, &BagItem{
Value: 6,
Weight: 2,
})
bagItem = append(bagItem, &BagItem{
Value: 7,
Weight: 2,
})
cmd := NewKnapsackOptimization(bagItem, 12)
cmd1 := NewKnapsackOptimization(bagItem, 12)
cmd2 := NewKnapsackOptimization(bagItem, 12)
fmt.Print(cmd.OptimizePackageByRecursion()) //通过递归解背包问题
fmt.Println(cmd1.KnapsackCycle()) //通过逆序解背包问题.
fmt.Println(cmd2.KnapsackCycleSimple()) //通过一维数组来解决背包问题
}