每瓶啤酒2元,2个空酒瓶或4个瓶盖可换1瓶啤酒。10元最多可喝多少瓶啤酒? php
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了每瓶啤酒2元,2个空酒瓶或4个瓶盖可换1瓶啤酒。10元最多可喝多少瓶啤酒? php,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1876字,纯文字阅读大概需要3分钟。
内容图文
//根据题意设定初始变量值
//然后 一个while循环去操作
//每换得一个瓶子,换的那个减去要做相应的数目
//然后喝了啤酒后, 各数量加1
//直到不符合要求。跳出循环
1 class Beer{ 2 3 protected $uni_gai = 4; //每4个瓶盖1瓶 4protected$uni_bottle = 2; //每两个瓶子换1瓶 5protected$uni_beer = 2; //每瓶2块钱 6protected$rs = array();//存取结果 7protected$total = 0; //当前买了啤酒的数量 8protected$gai = 0; //当前有多少个啤酒 9protected$empty_bottle = 0; //空瓶子1011publicfunction __construct($money){ 12$cur = $money / $this->uni_beer; 13$this->total = $cur; 14$this->gai = $cur; 15$this->empty_bottle = $cur; 16 } 1718publicfunction run(){ 19while($this->gai > 0 || $this->empty_bottle > 0){ 20if($this->gai >= $this->uni_gai){ 21$this->deal_num(‘gai‘); 22 } 23if($this->empty_bottle >= $this->uni_bottle){ 24$this->deal_num(‘empty_bottle‘); 25 } 2627$this->check_overflow(); 28 } 29return$this->rs; 30 } 3132publicfunction deal_num($type){ 33if($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 } 42publicfunction check_overflow(){ 43if($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 } 51publicfunction _print(){ 52echo ‘gai:‘, $this->gai; 53echo ‘<br>‘; 54echo ‘empty_bottle:‘, $this->empty_bottle; 55echo ‘<br>‘; 56echo ‘total‘, $this->total; 57echo ‘<hr>‘; 58 } 59} 6061$peer = new Beer(10); 62$rs = $peer->run(); 63print_r($rs);
打印的结果是: Array ( [gai] => 3 [empty_bottle] => 1 [total] => 15 )
盖子 3个, 空瓶子 1个, 总共喝了15瓶啤酒
原文:http://www.cnblogs.com/songbyjson/p/5137896.html
内容总结
以上是互联网集市为您收集整理的每瓶啤酒2元,2个空酒瓶或4个瓶盖可换1瓶啤酒。10元最多可喝多少瓶啤酒? php全部内容,希望文章能够帮你解决每瓶啤酒2元,2个空酒瓶或4个瓶盖可换1瓶啤酒。10元最多可喝多少瓶啤酒? php所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。