Selaa lähdekoodia

add version2 of drink

miles@Oscar 9 vuotta sitten
vanhempi
commit
39d850031f
2 muutettua tiedostoa jossa 44 lisäystä ja 0 poistoa
  1. 1 0
      drink/README.md
  2. 43 0
      drink/drink_zhihu.php

+ 1 - 0
drink/README.md

@@ -0,0 +1 @@
+每瓶啤酒2元,2个空酒瓶或4个瓶盖可换1瓶啤酒。10元最多可喝多少瓶啤酒?

+ 43 - 0
drink/drink_zhihu.php

@@ -0,0 +1,43 @@
+<?php
+
+class Drink
+{
+
+    public $num = 5;
+
+    public $topUnit = 4;
+
+    public $bottleUnit = 2;
+
+    public $leftTop;
+
+    public $leftBottle;
+
+    public $sum = 0;
+
+    public function __construct()
+    {
+        $this->sum = $this->num + $this->cal($this->num, $this->num);
+    }
+
+    public function cal($top, $bottle)
+    {
+        if ($top >= $this->topUnit || $bottle >= $this->bottleUnit) {
+            $this->leftTop = intval($top / $this->topUnit) + $top % $this->topUnit + intval($bottle / $this->bottleUnit);
+            $this->leftBottle = intval($bottle / $this->bottleUnit) + $bottle % $this->bottleUnit + intval($top / $this->topUnit);
+            $this->sum = intval($top / $this->topUnit) + intval($bottle / $this->bottleUnit) + $this->cal($this->leftTop, $this->leftBottle);
+            
+            return $this->sum;
+        } else {
+            return 0;
+        }
+    }
+
+    public function __toString()
+    {
+        return (string) $this->sum;
+    }
+}
+
+$c = new Drink();
+echo $c;