2 RMB for each bottle of beer, 2 empty bottles or 4 bottle caps can be changed to 1 bottle of beer. How many bottles of beer can I drink at most for 10 yuan? Php
// Set the initial variable value based on the meaning of the question. // then perform operations in a while loop. // for each change, replace the variable with the variable minus the corresponding number. // after drinking the beer, add 1 to each quantity until it does not meet the requirements. Jump out of Loop 1 class Beer {2 3 protected $ uni_gai = 4; // every 4 Bottle Caps 1 bottle 4 protected $ uni_bottle = 2; // Change 1 bottle for each two bottles 5 protected $ uni_beer = 2; // Change 2 yuan for each bottle 6 protected $ rs = array (); // access result 7 protected $ total = 0; // number of currently purchased beer 8 protected $ gai = 0; // Number of beer 9 protected $ empty_bottle = 0; // empty bottle 10 11 public function _ construct ($ money) {12 $ cur = $ money/$ this-> uni_beer; 13 $ this-> total = $ cur; 14 $ this-> gai = $ cur; 15 $ this-> empty_bottle = $ cur; 16} 17 18 public function run () {19 while ($ this-> gai> 0 | $ this-> empty_bottle> 0) {20 if ($ this-> gai >=$ this-> uni_gai) {21 $ this-> deal_num ('gai'); 22} 23 if ($ this-> empty_bottle >=$ this-> uni_bottle) {24 $ this-> deal_num ('empty _ bottle'); 25} 26 27 $ this-> check_overflow (); 28} 29 return $ this-> rs; 30} 31 32 public function deal_num ($ type) {33 if ($ type = 'gai') {34 $ this-> gai-= $ this-> uni_gai; 35} else {36 $ this-> empty_bottle-= $ this-> uni_bottle; 37} 38 $ this-> gai ++; 39 $ this-> empty_bottle ++; 40 $ this-> total ++; 41} 42 public function check_overflow () {43 if ($ this-> gai <$ this-> uni_gai & $ this-> empty_bottle <$ this-> uni_bottle) {44 $ this-> rs ['gai'] = $ this-> gai; 45 $ this-> rs ['total'] = $ this-> total; 46 $ this-> rs ['empty _ bottle'] = $ this-> empty_bottle; 47 $ this-> gai = 0; 48 $ this-> empty_bottle = 0; 49} 50} 51 public function _ print () {52 echo 'Gai: ', $ this-> gai; 53 echo'
'; 54 echo 'empty _ bottle:', $ this-> empty_bottle; 55 echo'
'; 56 echo 'total', $ this-> total; 57 echo ''; 58} 59} 60 61 $ peer = new Beer (10 ); 62 $ rs = $ peer-> run (); 63 print_r ($ rs );
The output is: Array ([gai] => 3 [empty_bottle] => 1 [total] => 15)
3 covers, 1 empty bottle, 15 beer in total