The C language solves the problem of buying a hundred chickens for a hundred dollars.

 A rooster is worth 5 renminbi, a hen is worth 3 renminbi, and three chicks are worth 1 renminbi. Now you need to spend a hundred renminbi to buy a hundred chickens. How many roosters, hens, and chicks are there?

The problem is very simple, and the answer is not difficult. The difficulty lies in solving a little problem every day. Let's start accumulating~
(1) If you only buy roosters with a hundred dollars, you can buy up to 20 roosters, but the title requires you to buy 100 roosters. From this we can see that the number of roosters purchased should be between 0 and 20.
(2) Similarly, the number of hens purchased should be between 0 and 33.
(3) It is advisable to set the number of cocks, hens, and chicks as cock, hen, and chicken respectively, then cock+hen+chicken=100, so the problem of buying a hundred chickens for a hundred dollars is transformed into a problem of solving indefinite
     equations .
 

#include"stdio.h"

int main() 
{
    int cock = 0;
    int hen = 0;
    int chicken = 0;
    for (cock = 0; cock <= 20; cock++)
    {
        for (hen = 0; hen < 33; hen++)
        {
            for (chicken = 1; chicken < 300; chicken++)
            {
                if (cock + hen + chicken == 100 && (cock * 5 + hen * 3 + chicken / 3) == 100  && chicken % 3 == 0)
                {
                    printf("公鸡买了%d只,  母鸡买了%d只,  小鸡买了%d只\n", cock, hen, chicken);
                }
            }
        }
    }
    return 0;
}

Running result display:

 

Guess you like

Origin blog.csdn.net/xingyuncao520025/article/details/131222778