This article describes the problem of 1,000 bottles of water in C# algorithm design. Share it for your reference. The details are as follows:
The title is as follows:Suppose there is N bottles of water (of course N>0)
An empty bottle you can get after drinking one bottle
And every 3 empty bottles can be replaced with 1 bottle of water, and after drinking it, you will get an empty bottle.
Ask how many bottles of water can you drink in total, and how many empty bottles are left in the end?
The code is as follows:
private int Water(int n, int emptyQty) { ("Drink" + n + "Bottle of water, more" + emptyQty + "Empty bottle."); if (n + emptyQty < 3) //If you have finished drinking water + empty bottles without 3, then you have finished drinking it { ("many" + (n + emptyQty) + "Empty bottle."); return n; } int a = (n + emptyQty) / 3; //If you drink it, you can change to a bottle of water int b = (n + emptyQty) % 3; //A few empty bottles left return n + Water(a, b); }
Assume that it is 1000 bottles at first, call the method
int sum = Water(1000,0); ("Drink" + sum + "Bottle of water");
I hope this article will be helpful to everyone's C# programming.