java程式實現一個有趣的買啤酒問題

來源:互聯網
上載者:User

標籤:money   public   java程式   package   啤酒   

一:問題描述

      啤酒2元一瓶,4個瓶蓋換一瓶啤酒,2個空瓶換一瓶啤酒,問10元能喝幾瓶啤酒?

二:問題分析

      針對上述問題,分兩種情況:一種是有剩餘空瓶和瓶蓋且不夠兌換的情況;另一種是當剩餘瓶蓋或空瓶不夠兌換時,老闆可以配合湊夠;我們分別來算出喝多少瓶啤酒。【結果為:喝完第15瓶時有剩餘1個空瓶3個蓋不夠兌換;如果老闆配合,喝20瓶啤酒正好無剩餘瓶蓋和空瓶】

三:程式實現

     先實現一個啤酒類:

package com.challenge.beer;public class Beer { /**  * 2元1瓶啤酒  * 4個蓋子換1瓶酒  * 2個空瓶換1瓶酒  * 10元可以喝幾瓶酒  */ public int Money; public int Gai; public int Empty; public int beer;  public int borrowGai; public int borrowEmpty; int Count1=0,Count2=0;   public Beer(int money, int gai, int empty, int beer) {  super();  Money = money;  Gai = gai;  Empty = empty;  this.beer = beer; } public Beer(int borrowGai, int borrowEmpty) {  super();  this.borrowGai = borrowGai;  this.borrowEmpty = borrowEmpty; }  public void Drink(int money,int gai,int empty){//喝酒函數  if(money>0){   beer=money/2;   Gai=money/2;   Empty=money/2;  }else{   Gai=gai;   Empty=empty;  }      System.out.println("開始喝酒:瓶蓋="+Gai+";空瓶="+Empty+";啤酒="+beer+"瓶"+"\n");    do{   if(Gai>=4){    int tmp1=Gai%4;    int tmp2=(int)Gai/4;    beer+=tmp2;    Gai=tmp1+tmp2;    Empty+=tmp2;    Count1++;    System.out.println("第"+Count1+"次瓶蓋換酒:瓶蓋="+Gai+";空瓶="+Empty+";啤酒="+beer+"瓶"+"\n");    }      if(Empty>=2){    int tmp3=(int)Empty/2;//換酒       int tmp4=Empty%2;//剩餘空瓶    Gai+=tmp3;//新蓋    Empty=tmp4+tmp3;       beer+=tmp3;       Count2++;    System.out.println("第"+Count2+"次空瓶換酒:瓶蓋="+Gai+";空瓶="+Empty+";啤酒="+beer+"瓶"+"\n");   }   System.out.println("喝了:"+beer+"瓶啤酒"+"\n");       //判斷是否要還空瓶或者瓶蓋   if(borrowGai >0||borrowEmpty>0){    Return();    }      }while(Gai>=4 || Empty>=2);      //借瓶蓋或者空瓶函數  Borrow(); }  public void Borrow(){//借瓶蓋或者空瓶函數    if(Gai==0 && Empty==0){   System.out.println("-----OVER!喝酒結束---");  }else{      if(4-Gai>2-Empty ){ //判斷借空瓶還是借蓋,借最少優先    int tmp=2-Empty;    Empty+=tmp;  //借空瓶    this.borrowEmpty+=tmp;    System.out.println("借了:"+this.borrowGai+"個蓋;"+tmp+"個空瓶"+"\n");     Drink(0,Gai,Empty);       }else{    int tmp=4-Gai;    Gai+=tmp;//借蓋    this.borrowGai+=tmp;    System.out.println("借了:"+tmp+"個蓋;"+"\n");     Drink(0,Gai,Empty);   }  } }  public void Return(){//規還空瓶/瓶蓋函數  //歸還瓶蓋  if(borrowGai>0){   if(Gai>0&&Gai>=borrowGai){    Gai-=borrowGai;    System.out.println("歸還:"+this.borrowGai+"個蓋;"+"\n");     borrowGai=0;   }  }  //歸還空瓶  if(borrowEmpty>0){   if(Empty>=borrowEmpty){    Empty-=borrowEmpty;    System.out.println("歸還:"+this.borrowEmpty+"個空瓶"+"\n");     borrowEmpty=0;   }  }   }}

       再通過主類調用該類對象:

 package com.challenge.beer;public class Main { /**  * @param args  */ public static void main(String[] args) {  // TODO Auto-generated method stub  Beer p=new Beer(10,0,0,0);  p.Drink(10,0,0); }}

三:程式輸出結果

開始喝酒:瓶蓋=5;空瓶=5;啤酒=5瓶

第1次瓶蓋換酒:瓶蓋=2;空瓶=6;啤酒=6瓶

第1次空瓶換酒:瓶蓋=5;空瓶=3;啤酒=9瓶

喝了:9瓶啤酒

第2次瓶蓋換酒:瓶蓋=2;空瓶=4;啤酒=10瓶

第2次空瓶換酒:瓶蓋=4;空瓶=2;啤酒=12瓶

喝了:12瓶啤酒

第3次瓶蓋換酒:瓶蓋=1;空瓶=3;啤酒=13瓶

第3次空瓶換酒:瓶蓋=2;空瓶=2;啤酒=14瓶

喝了:14瓶啤酒

第4次空瓶換酒:瓶蓋=3;空瓶=1;啤酒=15瓶

喝了:15瓶啤酒

借了:1個蓋;

開始喝酒:瓶蓋=4;空瓶=1;啤酒=15瓶

第4次瓶蓋換酒:瓶蓋=1;空瓶=2;啤酒=16瓶

第5次空瓶換酒:瓶蓋=2;空瓶=1;啤酒=17瓶

喝了:17瓶啤酒

歸還:1個蓋;

借了:0個蓋;1個空瓶

開始喝酒:瓶蓋=1;空瓶=2;啤酒=17瓶

第6次空瓶換酒:瓶蓋=2;空瓶=1;啤酒=18瓶

喝了:18瓶啤酒

歸還:1個空瓶

借了:2個蓋;

開始喝酒:瓶蓋=4;空瓶=0;啤酒=18瓶

第5次瓶蓋換酒:瓶蓋=1;空瓶=1;啤酒=19瓶

喝了:19瓶啤酒

借了:2個蓋;1個空瓶

開始喝酒:瓶蓋=1;空瓶=2;啤酒=19瓶

第7次空瓶換酒:瓶蓋=2;空瓶=1;啤酒=20瓶

喝了:20瓶啤酒

歸還:2個蓋;

歸還:1個空瓶

-----OVER!喝酒結束---

 

本文出自 “催花雨” 部落格,請務必保留此出處http://chenwen.blog.51cto.com/771416/1762473

java程式實現一個有趣的買啤酒問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.