首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >银行有无限供应3比索和5比索纸币的任务?

银行有无限供应3比索和5比索纸币的任务?
EN

Stack Overflow用户
提问于 2014-07-06 14:19:40
回答 1查看 68关注 0票数 1

,我正在做一个像这样的任务:

银行无限制地供应3比索和5比索纸币.显示这两种类型的纸币,银行可以支付任何数目的比索大于7。

答案是:

银行可以通过支付一张3比索纸币和一张5比索纸币支付8比索.

银行只需支付三张3比索的钞票就能支付9比索.

银行可以用两张5比索的钞票支付10比索.

银行可以通过支付3比索纸币支付11比索或更多比索,直到只有8比索、9比索或10比索才能支付,然后使用上述策略之一。(如果你从一个11或更大的数字中减去三个,你最终会到达8、9或10中的一个。)

我和试图在一个程序中实现这个功能.

到目前为止,我已经完成了以下工作:

代码语言:javascript
复制
class Money

  def initialize(price)
    @price = price.to_i
    validate(@price)
  end


  def validate(price)
    if price >= 8
      calculate(price)
    elsif price < 8
      puts "Minimum ammount is 8 pesos"
    end
  end


  def calculate(price)
    if price%5 === 0
      fives  = price/5
      threes = 0
    end

    result(fives, threes)
  end


  def result(fives, threes)
    puts "#{fives} five peso notes, #{threes} three peso notes."
  end

end


m = Money.new(ARGV.first)

你能帮我把这道数学题写进程序吗?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-06 14:32:34

代码语言:javascript
复制
def count(price)
  t = 0
  if price >= 11
    t = (price - 8) / 3
    price -= t * 3
  end
  if price == 8
    [t + 1, 1]
  elsif price == 9
    [t + 3, 0]
  elsif price == 10
    [t, 2]
  else
    fail "price too small"
  end
end

threes, fives = count(42)
puts "#{fives} five peso notes, #{threes} three peso notes."
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24596782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档