drink_zhihu.php 1006 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. class Drink
  3. {
  4. public $num = 5;
  5. public $topUnit = 4;
  6. public $bottleUnit = 2;
  7. public $leftTop;
  8. public $leftBottle;
  9. public $sum = 0;
  10. public function __construct()
  11. {
  12. $this->sum = $this->num + $this->cal($this->num, $this->num);
  13. }
  14. public function cal($top, $bottle)
  15. {
  16. if ($top >= $this->topUnit || $bottle >= $this->bottleUnit) {
  17. $this->leftTop = intval($top / $this->topUnit) + $top % $this->topUnit + intval($bottle / $this->bottleUnit);
  18. $this->leftBottle = intval($bottle / $this->bottleUnit) + $bottle % $this->bottleUnit + intval($top / $this->topUnit);
  19. $this->sum = intval($top / $this->topUnit) + intval($bottle / $this->bottleUnit) + $this->cal($this->leftTop, $this->leftBottle);
  20. return $this->sum;
  21. } else {
  22. return 0;
  23. }
  24. }
  25. public function __toString()
  26. {
  27. return (string) $this->sum;
  28. }
  29. }
  30. $c = new Drink();
  31. echo $c;