/** Subject: a total of 1000 bottles of soda * an empty bottle is obtained after each bottle is finished * one bottle of soda can be changed for each 3 empty bottles, and an empty bottle is obtained after drinking, how many bottles of soda can be consumed in total and how many empty bottles can be used in the end? **/1 static void Main () {2 int qs = 1000; // 1000 bottles of soda 3 int kp = 0; // empty bottle 4 int hd = 0; // drink 5 6 while (qs> = 1) {7 kp ++; 8 if (kp = 3) {9 qs ++; 10 kp = 0; 11} 12 hd ++; 13 qs --; 14 15} 16 Console. writeLine ("Drop: {0} bottle \ t empty bottle: {1}", hd, kp); 17} result:
Drop: 1499 empty bottles: 2
The push-by-push algorithm is as follows:
1000 bottles of soda:
--------------------------
1000% 3 = 333 1 drink 1000 bottles at a time, you can change 333 bottles of soda, the remaining 1 empty bottle = drink 1000 bottles of soda
333% 3 = 111 0 333 bottles of soda after drinking in exchange for 111 bottles of soda, the remaining 0 empty bottles = 1000 + 333 = 1333 bottles of soda
111% 3 = 37 0 111 bottles of soda in exchange for 37 bottles of soda, the remaining 0 empty bottles = 1333 + 111 = 1444 bottles of soda
37% 3 = 12 1 37 bottles of soda in exchange for 12 bottles of soda, the remaining 1 empty bottle = 1444 + 37 = 1481 bottles of soda
12% 3 = 4 0 12 bottles of soda in exchange for 4 bottles of soda, the remaining 0 empty bottles = 1481 + 12 = 1493 bottles of soda
4% 3 = 1 1 4 bottles of soda in exchange for 1 bottle of soda, the remaining 1 empty bottle = 1493 + 4 = 1497 bottles of soda
1 bottle of soda was drunk, and 1 empty bottle = 1497 + 1 = 1498 bottles of soda
Total number of empty bottles: 4; Change 1 bottle of soda; remaining 1 empty bottle = 1498 + 1 = 1499 bottles of soda
Total number of empty bottles: 2 empty bottles, cannot be exchanged for total steam: 1499 bottles of soda, 2 empty bottles.
Author "left"