贪心算法回顾
标签:array ++ import int out ack length 单位 over
各位好,贪心算法可以说是处处学到,被面试频频问道,接下来回顾以下,并上代码:
1 package com.clb.ai.algorithm;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.TreeMap;
7
8 /**
9 * 贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择。
10 * 也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解。
11 *
12 * 例题如下:
13 * 有一个背包,背包容量是M=150kg。有7个物品,物品不可以分割成任意大小。
14 * 要求尽可能让装入背包中的物品总价值最大,但不能超过总容量。
15 * 物品 A B C D E F G
16 * 重量 35kg 30kg 6kg 50kg 40kg 10kg 25kg
17 * 价值 10$ 40$ 30$ 50$ 35$ 40$ 30$
18 */
19 public class GreedyAlgo {
20
21 private static final int M = 150;
22
23 private static final char[] GOODS = {‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘};
24 private static final int[] WEIGHTS = {35, 30, 6, 80, 40, 10, 25};
25 private static final float[] COSTS = {10, 40, 30, 80, 35, 40, 30};
26
27 //按照价值大小进行排序
28 private static Map map = new TreeMap((o1,o2)->o1.price==o2.price?0:(o1.price));
29
30 public GreedyAlgo() {
31 for (int i=0; i) {
32 map.put(new Good(GOODS[i], WEIGHTS[i], COSTS[i]), GOODS[i]);
33 }
34 }
35
36 public List run(){
37 List resultList = new ArrayList();
38
39 int weight = 0;
40 while (weightM) {
41 boolean found = false;
42 for (Good v : map.keySet()) {
43 if (!resultList.contains(v) && v.weight weight) {
44 resultList.add(v);
45 weight += v.weight;
46 found = true;
47 }
48 }
49 if (!found) break;
50 }
51
52 return resultList;
53 }
54
55 public static void main(String[] args) {
56 List result = new GreedyAlgo().run();
57
58 System.out.println("可供选择的物品如下:");
59 for (Good key : map.keySet()) {
60 System.out.println(key);
61 }
62
63 System.out.println("通过贪心算法选取,得到的物品为:"+totalName(result));
64 float totalCost, totalWeight;
65 System.out.println("总重量:"+(totalWeight=totalWeight(result))+"\t总价值:"+(totalCost=totalCost(result))+"\t单位价值:"+(totalCost/totalWeight));
66 }
67
68 private static List totalName(List result) {
69 List characterList = new ArrayList();
70 result.forEach(key -> characterList.add(key.name));
71 return characterList;
72 }
73
74 private static float totalCost(List result) {
75 float sum = 0;
76 for (Good c : result) {
77 sum += c.cost;
78 }
79 return sum;
80 }
81 private static int totalWeight(List result) {
82 int sum = 0;
83 for (Good c : result) {
84 sum += c.weight;
85 }
86 return sum;
87 }
88
89 class Good {
90 char name;
91 int weight;
92 float cost;
93 float price;
94 public Good(char name, int weight, float cost) {
95 this.name = name;
96 this.weight = weight;
97 this.cost = cost;
98 this.price = cost/weight;
99 }
100
101 @Override
102 public String toString() {
103 final StringBuffer sb = new StringBuffer();
104 sb.append("名称:").append(name);
105 sb.append("\t重量:").append(weight);
106 sb.append("\t总价值:").append(cost);
107 sb.append("\t单位价值:").append(price);
108 return sb.toString();
109 }
110 }
111
112 }
打印结果如下:
1 可供选择的物品如下:
2 名称:C 重量:6 总价值:30.0 单位价值:5.0
3 名称:F 重量:10 总价值:40.0 单位价值:4.0
4 名称:B 重量:30 总价值:40.0 单位价值:1.3333334
5 名称:G 重量:25 总价值:30.0 单位价值:1.2
6 名称:D 重量:80 总价值:80.0 单位价值:1.0
7 名称:E 重量:40 总价值:35.0 单位价值:0.875
8 名称:A 重量:35 总价值:10.0 单位价值:0.2857143
9 通过贪心算法选取,得到的物品为:[C, F, B, G, E, A]
10 总重量:146.0 总价值:185.0 单位价值:1.2671233
贪心算法回顾
标签:array ++ import int out ack length 单位 over
原文地址:https://www.cnblogs.com/cheng2839/p/12884271.html
文章来自:
搜素材网的
编程语言模块,转载请注明文章出处。
文章标题:
贪心算法回顾
文章链接:http://soscw.com/index.php/essay/45908.html
评论