<script>
var proDuctNum = 5;
var MaxCapicity = 15;
var ValueArr = [4, 5, 10, 11, 13];
var Capicity = [3, 4, 7, 8, 9];
function Package_Value(Num, MaxCapicity, ValueArr, CaArr) {
var result = [];
for (var i=0;i<MaxCapicity;i++) {
result[i]=0;
}
for (var i=0;i<Num;i++) {
for (var j=MaxCapicity;j>CaArr[i];j--) {
//一维数组实现背包的核心在于:背包容量
//开始直接用最大容量装,然后追溯小于最大容量时但是却大于最大容量装的价值方法
result[j]=Max(result[j],result[j-CaArr[i]]+ValueArr[i])
}
}
return result;
}
function Max (num1,num2) {
return (num1>num2)?num1:num2;
}
function Func_sort(arr){
var Max=0;
var temp=0;
arr.sort(function(num1,num2){
return num1-num2;
})
temp=arr[arr.length-1];
if (temp>Max) {
Max=temp;
}
return Max;
}
console.log("最大结果是"+Func_sort(Package_Value(proDuctNum, MaxCapicity, ValueArr, Capicity)));
</script>