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();