Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,43 @@
|
|||
knapsack<- function(Value, Weight, Objects, Capacity){
|
||||
Fraction = rep(0, length(Value))
|
||||
Cost = Value/Weight
|
||||
#print(Cost)
|
||||
W = Weight[order(Cost, decreasing = TRUE)]
|
||||
Obs = Objects[order(Cost, decreasing = TRUE)]
|
||||
Val = Value[order(Cost, decreasing = TRUE)]
|
||||
#print(W)
|
||||
RemainCap = Capacity
|
||||
i = 1
|
||||
n = length(Cost)
|
||||
if (W[1] <= Capacity){
|
||||
Fits <- TRUE
|
||||
}
|
||||
else{
|
||||
Fits <- FALSE
|
||||
}
|
||||
while (Fits && i <= n ){
|
||||
Fraction[i] <- 1
|
||||
RemainCap <- RemainCap - W[i]
|
||||
i <- i+1
|
||||
#print(RemainCap)
|
||||
if (W[i] <= RemainCap){
|
||||
Fits <- TRUE
|
||||
}
|
||||
else{
|
||||
Fits <- FALSE
|
||||
}
|
||||
}
|
||||
#print(RemainCap)
|
||||
if (i <= n){
|
||||
Fraction[i] <- RemainCap/W[i]
|
||||
}
|
||||
names(Fraction) = Obs
|
||||
Quantity_to_take = W*Fraction
|
||||
Total_Value = sum(Val*Fraction)
|
||||
print("Fraction of available quantity to take:")
|
||||
print(round(Fraction, 3))
|
||||
print("KG of each to take:")
|
||||
print(Quantity_to_take)
|
||||
print("Total value of tasty meats:")
|
||||
print(Total_Value)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue