<?php

class Change
{

    /**
     *
     * @var 每多少盖子换一瓶汽水
     */
    public $capUnit;

    /**
     *
     * @var 每多少空瓶换一瓶汽水
     */
    public $bottelUnit;

    /**
     *
     * @var 总共金额
     */
    public $money;

    /**
     *
     * @var 汽水单价
     */
    public $unit;

    /**
     *
     * @var 总共能喝多少汽水
     */
    public $sum;

    public function __construct()
    {
        $this->money = 10;
        $this->unit = 2;
        $this->capUnit = 4;
        $this->bottelUnit = 2;
    }

    protected function capToWater($cap)
    {
        return intval($cap / $this->capUnit);
    }

    protected function capLeft($cap)
    {
        return $cap % $this->capUnit;
    }

    protected function bottleToWater($bottle)
    {
        return intval($bottle / $this->bottelUnit);
    }

    protected function bottelLeft($bottle)
    {
        return $bottle % $this->bottelUnit;
    }

    public function doCal()
    {
        $this->sum = intval($this->money / $this->unit);
        return $this->cal($this->sum, 0, 0, 0);
    }

    protected function cal($num, $lastNum, $left_cap, $left_bottle)
    {
        $cap = $num + $left_cap;
        $bottle = $num + $left_bottle;
        
        $capToWater = $this->capToWater($cap);
        $bottleToWater = $this->bottleToWater($bottle);
        $getWater = $capToWater + $bottleToWater;
        $this->sum += $getWater;
        
        $left_cap = $this->capLeft($cap);
        $left_bottle = $this->bottelLeft($bottle);
        
        $capToWater_next = $this->capToWater($getWater + $left_cap);
        $bottleToWater_next = $this->bottleToWater($getWater + $left_bottle);
        
        if ($capToWater_next || $bottleToWater_next) {
            $getWater = $this->cal($getWater, $num, $left_cap, $left_bottle);
        }
        return $this->sum;
    }
}

$c = new Change();
echo $c->doCal();
