Skip to content

Instantly share code, notes, and snippets.

@marocchino
Last active December 16, 2015 12:09
Show Gist options
  • Save marocchino/5432578 to your computer and use it in GitHub Desktop.
Save marocchino/5432578 to your computer and use it in GitHub Desktop.
코인 문제 : "1원, 5원, 10원, 50원, 100원, 500원짜리가 각각 C1, C5, C10, C50, C100, C500개씩 있습니다. 가능한 적은 수의 동적으로 A원을 지불하려면, 몇 개의 동전이 있어야 할까요? 지불 방법은 적어도 1개는 존재한다고 가정합니다."
def count_coins(a,c1,c5,c10,c50,c100,c500)
coins, sum = Hash.new {|hash,key| hash[key] = eval("c#{key}") }, 0
[500,100,50,10,5,1].each {|c| coins[c] }
left_amont = coins.inject(a) do |amount,(coin,count)|
count=[count,amount/coin].min
sum += count
amount -= coin*count
end
if left_amont != 0
raise "지불방법이 없는데요?"
else
sum
end
end
count_coins a=700,c1=4,c5=5,c10=5,c50=5,c100=10,c500=0 # => 7
@marocchino
Copy link
Author

출처는 여기에요.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment