本文共 1249 字,大约阅读时间需要 4 分钟。
题目描述:
有编号分别为a,b,c,d,e的五件物品,它们的重量分别是2,2,6,5,4,它们的价值分别是6,3,5,4,6,现在给你个承重为10的背包,如何让背包里装入的物品具有最大的价值总和?
name | weight | value | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
a | 2 | 6 | 0 | 6 | 6 | 9 | 9 | 12 | 12 | 15 | 15 | 15 |
b | 2 | 3 | 0 | 3 | 3 | 6 | 6 | 9 | 9 | 9 | 10 | 11 |
c | 6 | 5 | 0 | 0 | 0 | 6 | 6 | 6 | 6 | 6 | 10 | 11 |
d | 5 | 4 | 0 | 0 | 0 | 6 | 6 | 6 | 6 | 6 | 10 | 10 |
e | 4 | 6 | 0 | 0 | 0 | 6 | 6 | 6 | 6 | 6 | 6 | 6 |
public class Main { public static void main(String[] args) { // TODO Auto-generated method stub final int packageWheight=10;//包的重量 Package[] pg={ new Package(6,2,"a"), new Package(3,2,"b"), new Package(5,6,"c"), new Package(4,5,"d"), new Package(6,4,"e") }; int[][] bestValues = new int[pg.length+1][packageWheight+1]; for(int i=0;i<=pg.length;i++){ for(int j=0;j<=packageWheight;j++){ if(i==0||j==0){ bestValues[i][j]=0;//临界情况 } else{ if(j
/********************************
* 本文来自博客 “李博Garvin“
* 转载请标明出处:
******************************************/