C # algorithm design about 1000 bottles of water problem

Source: Internet
Author: User

In this paper, we describe the problem of C # algorithm design about 1000 bottles of water. Share to everyone for your reference. Specific as follows:


The topics are as follows:

Suppose there are n bottles of water (of course n>0 bar)

An empty bottle that you can get after every bottle you drink.

And every 3 empty bottles can change 1 bottles of water, drink and then get an empty bottle,

How many bottles of water can I drink in total, and how many empty bottles are left at the end?


The code is as follows:

private int water (int n, int emptyqty) {Console.WriteLine ("Drink" + N + "bottle water, Multi" + Emptyqty + "empty bottle."); if (n + emptyqty < 3 )//If you finish drinking water + empty bottle is not 3, then drink the {Console.WriteLine ("many" + (n + emptyqty) + "Empty Bottle"); return n;} int a = (n + emptyqty)/3; Drank the convertible a bottle of water int b = (n + emptyqty)% 3; More than a few empty bottles return n + water (A, b);}

Assuming the first 1000 bottles, call the method

int sum = water (1000,0); Console.WriteLine ("Drink" + sum + "bottle of Water");

I hope this article is helpful to everyone's C # programming.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # algorithm design about 1000 bottles of water problem

This address: http://www.paobuke.com/develop/c-develop/pbk23150.html






Related content development--corporate transfer to User C # using performance counters to monitor network status WPF Bullet Definition window methods WinForm Implementation Control Input Method
C # Implementing Windows Form Restriction text box Input Method C # methods to implement Windows Form Countdown Method C # calls an Oracle stored procedure in ASP. NET MVC 5 uses X.PAGEDLIST.MVC for a paging tutorial ( PAGEDLIST.MVC)

C # algorithm design about 1000 bottles of water 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.