k-tokitoh

2019-06-02

Ruby手習い(再帰または算数)

アウトプットのネタに困ったらこれ!?Ruby 初心者向けのプログラミング問題を集めてみた(全 10 問) - give IT a try

上記記事のボーナスドリンク問題。

自分で書いたコード

class BonusDrink
  def self.calc(count)
    count + self.bonus_of(count)
  end

  private

  def self.bonus_of(count)
    return 0 if count < 3
    count / 3 + self.bonus_of(count / 3 + count % 3)
  end
end

他の回答例