Each bottle of beer is 2 yuan, 2 empty bottles or 4 caps can be replaced by 1 bottles of beer. 10 Yuan How many bottles of beer can I drink? Php

Source: Internet
Author: User

//Set initial variable value according to test instructions
Then a while loop to manipulate
Every change to a bottle, the one minus to do the corresponding number
And then after the beer, the numbers add 1.
Until it does not meet the requirements. Jump out of the loop
1 classbeer{2 3 protected $uni _gai= 4;//1 bottles per 4 caps4 protected $uni _bottle= 2;//1 bottles for every two bottles5 protected $uni _beer= 2;//2 bucks per bottle .6 protected $rs=Array();//Access Results7 protected $total= 0;//The quantity of beer currently bought8 protected $gai= 0;//How many beers are there at the moment?9 protected $empty _bottle= 0;//Empty BottleTen One Public function__construct ($money){ A $cur=$money/$this-Uni_beer; - $this->total =$cur; - $this->gai =$cur; the $this->empty_bottle =$cur; - } - - Public functionrun () { + while($this->gai > 0 | |$this->empty_bottle > 0){ - if($this->gai >=$this-Uni_gai) { + $this->deal_num (' Gai '); A } at if($this->empty_bottle >=$this-uni_bottle) { - $this->deal_num (' Empty_bottle '); - } - - $this-Check_overflow (); - } in return $this-rs; - } to + Public functionDeal_num ($type){ - if($type= = ' Gai '){ the $this->gai-=$this-Uni_gai; *}Else{ $ $this->empty_bottle-=$this-Uni_bottle;Panax Notoginseng } - $this->gai++; the $this->empty_bottle++; + $this->total++; A } the Public functionCheck_overflow () { + if($this->gai <$this->uni_gai &&$this->empty_bottle <$this-uni_bottle) { - $this->rs[' gai '] =$this-gai; $ $this->rs[' total ' =$this-Total ; $ $this->rs[' empty_bottle '] =$this-Empty_bottle; - $this->gai = 0; - $this->empty_bottle = 0; the } - }Wuyi Public function_print () { the Echo' Gai: ',$this-gai; - Echo' <br> '; Wu Echo' Empty_bottle: ',$this-Empty_bottle; - Echo' <br> '; About Echo' Total ',$this-Total ; $ Echo' ; - } - } - A $peer=NewBeer (10); + $rs=$peer-run (); the Print_r($rs);

The result is: Array ([Gai] = 3 [Empty_bottle] = 1 [total] + 15)

Cover 3, Empty bottle 1, drink 15 bottles of beer in total

Each bottle of beer is 2 yuan, 2 empty bottles or 4 caps can be replaced by 1 bottles of beer. 10 Yuan How many bottles of beer can I drink? Php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.