[C# Exercise] Three Coke bottles can be exchanged for one Coke bottle, and now there are 364 Coke bottles. Ask how many bottles of Coke can be drunk in total, and how many empty bottles are left?

Topic: 3 Coke bottles can be exchanged for 1 Coke bottle, now there are 364 Coke bottles.

Question: How many bottles of Coke can be drunk in total, and how many empty bottles are left!

First look at the topic, this is a math problem

(Good guy, I didn't expect to have math problems qaq)

Now take out the scratch paper and write together:

Coke bottles can be exchanged for  Coke bottle,

Then  364  bottles of Coke can be exchanged for  364/3  bottles of Coke (since both sides of / are integers , the result must also be an integer),

364%  of the remaining  3 bottles of Coke can't be exchanged, because there are less than 3 bottles~


So the first wave,

The total number of drinks is: (drink + change)

364 + 364/3

364 +121 =485 

The remaining empty bottles are: (replaced + not enough to replace the rest)

364/3 + 364%3

 121+ 1 = 122

Come on the second wave,

The total number of drinks is: (the first wave of drinking + the remaining empty bottles of the first wave/3)

(364 + 364/3)+(364/3 + 364%3)/ 3 

485 + 122 / 3 = 525

The remaining empty bottles are: (the remaining empty bottles of the first wave/3 + the remaining empty bottles of the first wave%3)

(364/3 + 364%3)/3+(364/3 + 364%3 )%3

122 / 3 + 122 % 3 = 42

At this time, you can find that you will drink more and more Coke, and the remaining empty bottles will be less and less;

So when will this cycle end, that is, when the empty bottles are not enough to replace with new Coke, that is, when the number of empty bottles is < 3;

It is known that there are 364 bottles of Coke, then set the number of Coke: int sum=364;

The number of bottles: int ping = sum; //The number of bottles is the same as the number of cola

At this time, a loop is performed, as long as the number of bottles is greater than or equal to 3 (can be exchanged for new Coke), then the loop is executed;

while (ping >= 3)
{
    sum += ping / 3;
    ping = ping / 3+ ping % 3;
}

Then output how many bottles of Coke you can drink in total, and how many empty bottles are left.

The source code is as follows:

int sum = 364;
int ping = sum;
while (ping >= 3)
{
    sum += ping / 3;
    ping = ping / 3+ ping % 3;
}
Console.WriteLine("一共可以喝多少瓶可乐:" + sum + "\n还剩多少瓶:" + ping);

The result of the operation is as follows:

 

 This topic is mainly to study mathematical algorithms, logic still needs to be studied hard!


Finally, a small function is added to the question, which can input the quantity and exchange method of Coke:

Console.WriteLine("现在有几瓶可乐?");
int sum = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("几瓶可乐能整一瓶?");
int value = Convert.ToInt32(Console.ReadLine());
int ping = sum;
while (ping >= value)
{
    sum += ping / value;
    ping = ping / value + ping % value;
}
Console.WriteLine("一共可以喝多少瓶可乐:" + sum + "\n还剩多少瓶:" + ping);

The result of running is like this:

 

 It's not easy to make, let's have a one-click three-in-a-row! !

Guess you like

Origin blog.csdn.net/xichi_12396/article/details/119345019