Created
December 7, 2013 14:52
-
-
Save inage/7843514 to your computer and use it in GitHub Desktop.
個数制限なしナップサック問題
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## 個数制限なしナップサック問題 | |
| ## http://rubyfiddle.com/riddles/9eb7a | |
| n = 3 | |
| w = [3,4,2] | |
| v = [4,5,3] | |
| W = 7 | |
| dp = Array.new(W+1,0) | |
| i=j=0 | |
| n.times{|i| | |
| j = w[i] | |
| while j <= W | |
| dp[j] = [dp[j],dp[j-w[i]]+v[i]].max | |
| j+=1 | |
| end | |
| } | |
| print dp[W] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment