Skip to content

Instantly share code, notes, and snippets.

@shoken0x
Last active November 19, 2015 02:06
Show Gist options
  • Save shoken0x/68e294b6685e887aa4e1 to your computer and use it in GitHub Desktop.
Save shoken0x/68e294b6685e887aa4e1 to your computer and use it in GitHub Desktop.
PCC Book: Knapsack Problem
# input
$n = 4
$w = [2, 1, 3, 2]
$v = [3, 2, 4, 2]
$W = 5
# 重さ$wと価値$vの組み合わせの品物が$n個ある
# 重さの総和が$Wを超えないように選んだときの価値の総和の最大値を求める
def rec(i, j)
if i == $n
res = 0
elsif j < $w[i]
res = rec(i + 1, j)
else
res = [rec(i + 1, j), rec(i + 1, j - $w[i]) + $v[i]].max
end
res
end
def slove
puts rec(0, $W)
end
slove
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment