小提莫来报到
美好的事情值得等待

问题:有一个容量固定的背包和一些重量和价值都已知的物品,求背包能装下价值最高的物品组合,每个物品只能装一次

定义物品实体

 1 /**
 2  * Description: 物品实体
 3  */
 4 public class MyGoods {
 5     //名称
 6     private String name;
 7     //重量
 8     private int weight;
 9     //价值
10     private int price;
11 
12     public MyGoods(String name, int weight, int price) {
13         this.name = name;
14         this.weight = weight;
15         this.price = price;
16     }
17 
18     public String getName() {
19         return name;
20     }
21 
22     public void setName(String name) {
23         this.name = name;
24     }
25 
26     public int getWeight() {
27         return weight;
28     }
29 
30     public void setWeight(int weight) {
31         this.weight = weight;
32     }
33 
34     public int getPrice() {
35         return price;
36     }
37 
38     public void setPrice(int price) {
39         this.price = price;
40     }
41 
42     @Override
43     public String toString() {
44         return "MyGoods{" +
45                 "name='" + name + '\'' +
46                 ", weight=" + weight +
47                 ", price=" + price +
48                 '}';
49     }
50 }
物品实体

定义背包实体

 1 import java.util.ArrayList;
 2 import java.util.List;
 3 
 4 /**
 5  * Description: 背包
 6  */
 7 public class MyPack {
 8     //装进来的物品名称
 9     private List<String> goodNames;
10     //已装容量
11     private int weight;
12     //剩余容量
13     private int hasWeight;
14     //装进来的物品价值
15     private int price;
16 
17     public MyPack(int hasWeight) {
18         this.weight = 0;
19         this.hasWeight = hasWeight;
20         this.price = 0;
21         goodNames = new ArrayList<>();
22     }
23 
24     public boolean addGoods(MyGoods goods) {
25         if (canEntry(goods)) {
26             this.hasWeight -= goods.getWeight();
27             this.price += goods.getPrice();
28             this.weight += goods.getWeight();
29             goodNames.add(goods.getName());
30             return true;
31         }
32         return false;
33 
34     }
35 
36     public boolean canEntry(MyGoods goods) {
37         return this.hasWeight >= goods.getWeight();
38     }
39 
40     public List<String> getGoodNames() {
41         return goodNames;
42     }
43 
44     public void setGoodNames(List<String> goodNames) {
45         this.goodNames = goodNames;
46     }
47 
48     public int getWeight() {
49         return weight;
50     }
51 
52     public void setWeight(int weight) {
53         this.weight = weight;
54     }
55 
56     public int getHasWeight() {
57         return hasWeight;
58     }
59 
60     public void setHasWeight(int hasWeight) {
61         this.hasWeight = hasWeight;
62     }
63 
64     public int getPrice() {
65         return price;
66     }
67 
68     public void setPrice(int price) {
69         this.price = price;
70     }
71 
72     @Override
73     public String toString() {
74         return "MyPack{" +
75                 "goodNames=" + goodNames +
76                 ", weight=" + weight +
77                 ", hasWeight=" + hasWeight +
78                 ", price=" + price +
79                 '}';
80     }
81 }
背包实体

背包最大价值的组合

 1 public static MyPack maxPrice(MyPack pack, List<MyGoods> goods) {
 2     //物品按重量从小到大排序
 3     goods.sort(Comparator.comparing(MyGoods::getWeight));
 4     //所有可装下的物品组合
 5     List<MyPack> tmps = new ArrayList<>();
 6     for (int i = 0; i < goods.size(); i++) {
 7         MyPack temPack = new MyPack(pack.getHasWeight());
 8         if (temPack.addGoods(goods.get(i))) {
 9             if(i == goods.size()-1){
10                 tmps.add(temPack);
11                 break;
12             }
13             int t = 0;
14             for (int j = i + 1; j < goods.size(); j++) {
15                 if (!temPack.addGoods(goods.get(j))) {
16                     tmps.add(temPack);
17                     if(t==0){
18                         temPack = new MyPack(pack.getHasWeight());
19                         if(goods.get(i).getWeight() + goods.get(j).getWeight() <= temPack.getHasWeight()){
20                             temPack.addGoods(goods.get(i));
21                             j--;
22                             t=1;
23                         }
24                     }
25                 }else {
26                     t=0;
27                     if(j==goods.size()-1){
28                         tmps.add(temPack);
29                     }
30                 }
31             }
32 
33         }else {
34             break;
35         }
36     }
37     if (tmps.size() > 0) {
38         //按照价值从大到小排序
39         tmps.sort(Comparator.comparing(MyPack::getPrice).reversed());
40         //利用java8流操作取出价值最大的
41         return tmps.stream().max(Comparator.comparingInt(MyPack::getPrice)).get();
42     }
43     return pack;
44 }

测试的例子

 1  public static void main(String[] args) {
 2         //定义背包容量是10
 3         MyPack myPack = new MyPack(10);
 4         //定义物品集合
 5         List<MyGoods> goods = new ArrayList<>();
 6         goods.add(new MyGoods("b", 3, 3));
 7         goods.add(new MyGoods("d", 6, 5));
 8         goods.add(new MyGoods("a", 2, 1));
 9         goods.add(new MyGoods("c", 2, 2));
10         goods.add(new MyGoods("e", 7, 3));
11         //价值最高的组合
12         MyPack maxPricePack = maxPrice(myPack, goods);
13         //打印结果
14         System.out.println(maxPricePack.toString());
15     }

例子打印结果

1 MyPack{goodNames=[b, d], weight=9, hasWeight=1, price=8}

  

 

posted on 2021-08-17 14:16  小提莫来报到  阅读(53)  评论(0编辑  收藏  举报