Skip to content

Instantly share code, notes, and snippets.

@myokoym
Created August 15, 2012 13:43
Show Gist options
  • Save myokoym/3360286 to your computer and use it in GitHub Desktop.
Save myokoym/3360286 to your computer and use it in GitHub Desktop.
ある金額になる硬貨の組み合わせを求めるメソッド
#! 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