日々精進

新しく学んだことを書き留めていきます

プログラミングコンテストチャレンジブック演習「硬貨の問題」

今回のお題はこちら。

A = 620 #input
c500 = 2 #input
c100 = 0 #input
c50 = 3 #input
c10 = 1 #input
c5 = 2 #input
c1 = 3 #input
coins = [[c500, 500], [c100, 100], [c50, 50], [c10, 10], [c5, 5], [c1, 1]]

leftMoney = A
paidNumOfCoins = 0
coins.each do |coin|
  paidNumOfCoins += [(leftMoney / coin[1]).truncate, coin[0]].min
  leftMoney = leftMoney - coin[1] * [(leftMoney / coin[1]).truncate, coin[0]].min
end

puts paidNumOfCoins