,我正在做一个像这样的任务:
银行无限制地供应3比索和5比索纸币.显示这两种类型的纸币,银行可以支付任何数目的比索大于7。
答案是:
银行可以通过支付一张3比索纸币和一张5比索纸币支付8比索.
银行只需支付三张3比索的钞票就能支付9比索.
银行可以用两张5比索的钞票支付10比索.
银行可以通过支付3比索纸币支付11比索或更多比索,直到只有8比索、9比索或10比索才能支付,然后使用上述策略之一。(如果你从一个11或更大的数字中减去三个,你最终会到达8、9或10中的一个。)
我和试图在一个程序中实现这个功能.
到目前为止,我已经完成了以下工作:
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)你能帮我把这道数学题写进程序吗?谢谢。
发布于 2014-07-06 14:32:34
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."https://stackoverflow.com/questions/24596782
复制相似问题