Go Language Foundation, a small example every day-hundred money hundred chicken

Source: Internet
Author: User
Case Description: The ancient Chinese mathematician Zhang Chujian in his "calculation of the book" put forward a famous "hundred Money Hundred Chicken problem": A rooster worth five money, a hen worth three money, three chickens worth a money, now to use hundred money to buy hundred chickens, ask rooster, hen, chicken how many only? Case study: If you buy only one chicken with 100 bucks, then the rooster is up to 20, the hen up to 33, and the chick up to 300. But the topic requires buying 100, so the number of chicks between 0~100, the number of cocks between 0~20, the number of hens between 0~33. We set the number of cocks, hens and chickens as cock, hen, chicken, through the above analysis: (1) 0<=cock<=20; (2) 0<=hen<=33; (3) 0<=chicken<= (4) cock+hen+chicken=100; (5) 5*cock+3*hen+chicken/3=100. At the same time, it is known that the number of hens, chickens and cocks is limited to each other, and this can be solved by using a three-layer loop nesting. Before implementing a case, learn the knowledge needed to complete the program. Required Knowledge: Loop nesting: In the Go language, we have a dedicated loop structure is the for structure (the go language only for the loop structure, no while,do-while structure), the basic syntax structure is as follows:! [1.png] (https://static.studygolang.com/180805/87bd173bffd19213b88e5393286174af.png) Code implementation: To improve efficiency, the algorithm can be optimized. When the number of cocks and hens is determined, the number of chicks is fixed to 100-cock-hen, code as follows:! [2.png] (https://static.studygolang.com/180805/36ed6f8946161ce7dbd194126734b141.png) Get execution Result: Rooster: 0 Hen: 25 chick: 75 Rooster: 4 Hen: 18 Chick: 78 Cocks: 8 hens: 11 Chicks: 81 Cocks: 12 hens: 4 chicks: 84,141 Reads
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.