Java program to implement an interesting buy beer problem

Source: Internet
Author: User

One: Problem description

Beer 2 yuan a bottle, 4 caps for a bottle of beer, 2 empty bottles for a bottle of beer, ask 10 yuan to drink a few bottles of beer?

Two: Problem analysis

In view of the above problems, there are two cases: one is the case of empty bottles and caps and not enough convertibility, the other is that when the remaining caps or empty bottles are not enough to redeem, the boss can match up enough; we'll figure out how many beers to drink. "The result: there were 1 empty bottles and 3 caps in the 15th bottle, not enough to redeem, and if the boss cooperated, 20 bottles of beer had no remaining caps and empty bottles."

Three: Program implementation

First, a beer class is implemented:

Package com.challenge.beer;public class beer { /**  * 2 1 bottles of beer    * 4 a lid for 1 bottles of wine   * 2 empty bottles for 1 bottles of wine   * 10 yuan can drink a few bottles of wine   */ 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) {//Drinking function   if (money>0) {   beer=money/2;   gai=money/2;    Empty=money/2;  }else{   Gai=gai;   Empty=empty;   }      system.out.println ("Start drinking: bottle cap =" +gai+ "; Empty bottle =" +empty+ "; beer =" +beer+ "bottle "+" \ 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+" sub-cap for wine: cap = "+gai+"; Empty bottle = "+empty+"; beer = "+beer+" bottle "+" \ n ");     }       if (empty>=2) {    int tmp3= (int) empty/2;//Wine Exchange        int tmp4=empty%2;//remaining empty bottle     gai+=tmp3;//new Lid     empty=tmp4+tmp3;       beer+=tmp3;        Count2++;    system.out.println ("+count2+" second empty bottle for wine: bottle cap = "+gai+"; Empty bottle = "+empty+"; beer = "+beer+" bottle "+" \ n ");    }   system.out.println (" Drink: "+beer+" bottle of beer "+" \ n ");        //determine whether to empty bottles or caps    if (borrowgai >0| | borrowempty>0) {    return ();    }       }while (gai>=4 | |  empty>=2)       //The bottle cap or empty bottle function   borrow (); }   Public void borrow () {//borrow bottle cap or empty bottle function     if (gai==0 && empty==0) {    SYSTEM.OUT.PRINTLN ("-----over! drink Ends---");  }else{       if (4-gai>2-empty ) { //judge to borrow empty bottle or borrow cover, take the least priority     int tmp=2-Empty;     empty+=tmp;  //lent empty Bottle     this.borrowempty+=tmp;    system.out.println ("lent:" + this.borrowgai+ "lid", "+tmp+" Empty Bottle "+" \ n ");      drink (0,gai,empty);        }else{    int tmp=4-gai;    gai+=tmp;//Borrow cover      this.borrowgai+=tmp;    system.out.println ("Borrowed:" +tmp+ "a lid;" + "\ n");      drink (0,gai,empty);    }  } }  public void  return () {//gauge also empty bottle/Cap function   //return cap   if (borrowgai>0) {   if (gai>0& &gai>=borrowgai) {    gai-=borrowgai;    system.out.println ("return: "+this.borrowgai+" cover; "+" \ n ");     borrowgai=0;   }  }   //return empty Bottle   if (borrowempty>0) {   if (empty>=borrowempty) {     empty-=borrowemptY;    system.out.println ("Return:" +this.borrowempty+ "empty Bottle" + "\ n");      borrowempty=0;   }  }   }}

The class object is then called from the main class:

Package Com.challenge.beer;public class Main {/** * @param args */public static void Main (string[] args) {//TODO Au  to-generated method Stub Beer p=new Beer (10,0,0,0); P.drink (10,0,0); }}

Three: Program output results

Start Drinking: bottle cap = 5; Empty bottle = 5; beer = 5 Bottles

1th time bottle cap for wine: bottle cap = 2; empty bottle = 6; beer = 6 Bottles

The 1th time empty bottle for wine: bottle cap = 5; Empty bottle = 3; beer = 9 Bottles

Drink: 9 Bottles of beer

2nd time bottle cap for wine: bottle cap = 2; empty bottle = 4; beer = 10 Bottles

The 2nd time empty bottle for wine: bottle cap = 4; empty bottle = 2; beer = 12 Bottles

Drink: 12 Bottles of beer

3rd time Bottle cap for wine: bottle cap = 1; empty bottle = 3; beer = 13 bottles

The 3rd time Empty bottle for wine: bottle cap = 2; empty bottle = 2; beer = 14 Bottles

Drink: 14 Bottles of beer

The 4th time Empty bottle for wine: bottle cap = 3; empty bottle = 1; beer = 15 Bottles

Drink: 15 Bottles of beer

Lent: a cover;

Start Drinking: bottle cap = 4; Empty bottle = 1; beer = 15 Bottles

4th time Bottle cap for wine: bottle cap = 1; empty bottle = 2; beer = 16 Bottles

The 5th time empty bottle for wine: bottle cap = 2; empty bottle = 1; beer = 17 bottles

Drink: 17 Bottles of beer

To return: a lid;

Lent: 0 caps; 1 empty Bottles

Start Drinking: bottle cap = 1; empty bottle = 2; beer = 17 bottles

The 6th time empty bottle for wine: bottle cap = 2; empty bottle = 1; beer = 18 Bottles

Drink: 18 Bottles of beer

Return: 1 Empty Bottles

Lent: 2 covers;

Start Drinking: bottle cap = 4; empty bottle = 0; beer = 18 Bottles

5th time Bottle cap for wine: bottle cap = 1; empty bottle = 1; beer = 19 Bottles

Drink: 19 Bottles of beer

Lent: 2 caps; 1 empty Bottles

Start Drinking: bottle cap = 1; empty bottle = 2; beer = 19 Bottles

The 7th time empty bottle for wine: bottle cap = 2; empty bottle = 1; beer = 20 Bottles

Drink: 20 Bottles of beer

return: 2 caps;

Return: 1 Empty Bottles

-----over! Drinking Ends---

This article is from the "Reminder Flower Rain" blog, please make sure to keep this source http://chenwen.blog.51cto.com/771416/1762473

Java program to implement an interesting buy beer problem

Related Article

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.