Typical C language questions: buy a chicken for a hundred dollars

Source: Internet
Author: User

How to buy a chicken for a hundred dollars

In the fifth century of money, Zhang qiujian, a Chinese ancient mathematician, put forward the "Hundred rooster problem" in the book "computing classics": A rooster worth five yuan and a rooster worth three yuan, three birds worth one. I want to buy a chicken for a hundred dollars. What are the geometric aspects of chicken Weng, chicken mother, and chicken chicks?

Analysis: assume there are X rooster, y hen, and Z chicks. That X + Y + z = 100, and the price is 5 * x + 3 * Y + z/3 = 100. You only need to satisfy the number of the two equations above. We can use C language 3 to repeat all the answers.

However, the number of chickens is a positive integer, and z/3 may produce decimals. Therefore, you may wish to set the money used to buy a chicken as a variable.

Set the number of brokers to int cock, the number of hens to cola, and the money used to buy chicks to p_chickens;

Also, the analysis equation shows that the number of rooster is <20, the number of hens is <= 33, and the money used to buy chicks is p_chickens;

So the program is as follows:

# Include <stdio. h> int main (void) {int C; // Number of cocks int forward; // Number of hens int p_chickens; // For (C = 0; C <20; C ++) for (cost = 0; Cost <33; Cost ++) for (p_chickens = 0; p_chickens <33; p_chickens ++) if (5 * C + 3 * queues + p_chickens = 100) & (C + rooster + 3 * p_chickens = 100) printf ("rooster has % d, hen has % d, and chicken has % d only \ n ", c, counts, 3 * p_chickens); Return 0 ;}

The answer is:

If the number of cocks cannot be 0, remove the first answer.

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.