C #: 1000 bottles of soda. How many bottles are there and how many empty bottles are there when I earn one bottle for every 3 bottles?

Source: Internet
Author: User

/** 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"

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.