贪心法找零钱

 

  def make_change(amount, coins = [])
    coins.sort! { |x, y| y <=> x }
    return coins.map!{ |coin|
      f = amount/coin
      amount %= coin
      Array.new(f){coin}
    }.flatten
  end

 

示例:

 

make_change(39, [10, 1, 5, 20])  # => [20, 10, 5, 1, 1, 1, 1]

你可能感兴趣的:(F#)