题目出处点这里
思路:构造一个金币类,按性价比从小到大进行排序即可,不过要注意可能有全部金币都取完了但是背包还没满的情况,此时直接输出maxPrice即可
代码如下(很好理解):
package greedy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class P2240 {
static ArrayList<coin> list = new ArrayList<coin>();
static double maxPrice = 0;
static double curWeight = 0;
static int index;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double N = sc.nextInt();
double T = sc.nextInt();
for (int i = 0; i < N; i++) {
list.add(new coin(sc.nextInt(), sc.nextInt()));
}
//这里需要自己写一个比较器,只对coin对象的xjb进行由大到小的排序
Collections.sort(list, new Comparator<coin>() {
public int compare(coin a, coin b) {
double s = (b.xjb - a.xjb);
if (s > 0) {
return 1;
} else if (s == 0) {
return 0;
} else {
return -1;
}
}
});
for (int i = 0; i < N; i++) {
if (curWeight + list.get(i).w > T) {
break;
}
curWeight = curWeight + list.get(i).w;
maxPrice = maxPrice + list.get(i).p;
index = i;
}
double restWeight = T - curWeight;// 剩余的容量
if (restWeight != 0 && index + 1 < N) {
maxPrice = maxPrice + restWeight * list.get(index + 1).p / list.get(index + 1).w;
System.out.printf("%.2f", maxPrice);
} else {// 注意当index + 1 == N 时,就说明东西都放进背包里了,此时有可能背包没放满,也有可能刚刚放满
System.out.printf("%.2f", maxPrice);
}
}
}
class coin {
double w;// 重量
double p;// 价值
double xjb;// 性价比
// 构造器
public coin(double w, double p) {
this.w = w;
this.p = p;
this.xjb = p / w;
}
}