Created
August 15, 2012 13:43
-
-
Save myokoym/3360286 to your computer and use it in GitHub Desktop.
ある金額になる硬貨の組み合わせを求めるメソッド
This file contains 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
#! ruby | |
# coins.sort.reverse! | |
def calc(sum, coins) | |
if coins.size == 1 | |
return (sum % coins[0] == 0) ? 1 : 0 | |
end | |
count = 0 | |
num = coins[0] | |
0.upto(sum / num) do |i| | |
count += calc(sum - (num * i), coins[1..-1]) | |
end | |
count | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment