10 Yuan Buy beer 2 empty bottles for a bottle of 4 caps for a bottle of classic algorithm

Source: Internet
Author: User
Tags static class
Reprint Please specify source: http://blog.csdn.net/li396864285/article/details/51519786

"Original" 10 Yuan to buy beer 2 empty bottles for a bottle of 4 bottles for a bottle, the classic algorithm original title: 10 Yuan to buy beer, 2 empty bottles can be exchanged for a bottle, 4 caps can be exchanged for a bottle, how many bottles can I drink altogether.

</pre><pre code_snippet_id= "1699391" snippet_file_name= "blog_20160527_1_8970255" name= "code" class= "Java"
>//10 yuan to buy 5 bottles of beer, 2 caps for a bottle, 4 empty bottles for a bottle, 10 money in the end can buy how many bottles of beer.
 /** * Created by Lee on 2016/5/27.
        */public class Bottletest {public static void main (string[] args) {int yourmoney = 10;
        Turnbeerinfo turnbeerinfo = new Turnbeerinfo ();
        Buybeer (Turnbeerinfo,yourmoney);
        int i = 0; while (turnbeerinfo.bottlenum>=2| |
        turnbeerinfo.covernum>=4) {compute (turnbeerinfo,++i); } System.out.println ("Drank a total of" +turnbeerinfo.drinksumnum+ "bottle of beer, and left" +turnbeerinfo.bottlenum+ "bottle," +turn
    Beerinfo.getcovernum () + "one bottle cap");
        public static void Buybeer (Turnbeerinfo turnbeerinfo,int money) {int beernum = MONEY/2;
        Turnbeerinfo.drinksumnum = Beernum;
        Turnbeerinfo.bottlenum = Beernum;
    Turnbeerinfo.covernum = Beernum;
      } public static void Compute (Turnbeerinfo turnbeerinfo,int i) {  System.out.println ("+i+" Redemption, you currently drink a total of "+turnbeerinfo.drinksumnum+" bottle of beer, and the remaining "+turnbeerinfo.bottlenum+" bottle, "
        +turnbeerinfo.getcovernum () + "one bottle cap"); if (turnbeerinfo.bottlenum/2>0) {int bottleturnnum =turnbeerinfo.bottlenum/2;//a bottle for bottleturnnum bottle and a bot Tleturnnum a bottle, bottleturnnum a lid turnbeerinfo.drinksumnum + = Bottleturnnum;//1: Add to drink total beer turnbeerinfo . Covernum + = BOTTLETURNNUM;//2: Cumulative Cap Quantity turnbeerinfo.bottlenum = Turnbeerinfo.bottlenum%2+bottleturnnum;//3: Recalculate The number of remaining bottles, please remember to convert the wine produced by the bottle, but also in time to accumulate} if (turnbeerinfo.covernum/4>0) {int coverturnnum = Turnbeerinf o.covernum/4;//the Coverturnnum bottle with a lid, and produced a coverturnnum bottle, coverturnnum a lid turnbeerinfo.drinksumnum + = CoverTurnN UM;//1: Cumulative Beer total Turnbeerinfo.bottlenum + = coverturnnum;//2: number of bottles turnbeerinfo.covernum = Turnbeer
    INFO.COVERNUM%4+COVERTURNNUM;//3: Recalculate the remaining cap quantity, please remember the newly converted wine produced by the lid, but also in time to accumulate} return;
    }public static class turnbeerinfo{private int bottlenum;//Number of remaining bottles, 2 bottles for 1 bottles of beer private int covernum;//number of remaining caps

        , 4 lids can be changed to 1 bottles of beer private int drinksumnum;
        public int Getbottlenum () {return bottlenum;
        } public void Setbottlenum (int bottlenum) {this.bottlenum = Bottlenum;
        } public int Getcovernum () {return covernum;
        } public void Setcovernum (int covernum) {this.covernum = Covernum;
        } public int Getdrinksumnum () {return drinksumnum;
        } public void Setdrinksumnum (int drinksumnum) {this.drinksumnum = Drinksumnum;
 }
    }

}

Please refer to the above code, where the key point is the calculation, the 3rd step, you must remember the newly converted wine produced by the bottle and the lid into the remainder.


Now, you can clearly know that when you have 10 dollars, you can drink up to how many bottles:

For the 1th redemption, you currently have a total of 5 bottles of beer, 5 bottles, 5 caps
For the 2nd redemption, you currently have a total of 8 bottles of beer, 4 bottles, 4 caps
For the 3rd redemption, you currently have a total of 11 bottles of beer, 3 bottles, 3 caps
For the 4th redemption, you currently have a total of 13 bottles of beer, 3 bottles, 1 caps
For the 5th redemption, you currently have a total of 14 bottles of beer, 2 bottles, 2 caps

A total of 15 bottles of beer, 1 bottles, 3 caps.


If you have 12 yuan, you can drink up to how many bottles:

For the 1th redemption, you currently have a total of 6 bottles of beer, 6 bottles, 6 caps
For the 2nd redemption, you currently have a total of 11 bottles of beer, 5 bottles, 3 caps
For the 3rd redemption, you currently have a total of 14 bottles of beer, 4 bottles, 2 caps
For the 4th redemption, you currently have a total of 17 bottles of beer, 3 bottles, 1 caps
For the 5th redemption, you currently have a total of 18 bottles of beer, 2 bottles, 2 caps

A total of 19 bottles of beer, 1 bottles, 3 caps.


It's amazing.

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.