C language realizes the question of buying a hundred chickens for a hundred dollars: how to buy 100 chickens?

C language realizes the question of buying a hundred chickens for a hundred dollars: how to buy 100 chickens?

The problem of buying a hundred chickens for a hundred dollars is a classic algorithm problem. The description of the title is as follows: The farmer has 100 yuan, and he needs to use the 100 yuan to buy 100 chickens, 5 yuan for a rooster, 3 yuan for a hen, and 3 chickens for 1 yuan. How should the farmer buy it? Maximize the number of chickens purchased?

To solve this problem, we can use a triple loop to simulate all possible buying situations and compare which of them can buy the most chickens.

First, we can use a loop to enumerate the number i of cocks bought, and the value of i ranges from 0 to 20 (because cocks can buy up to 20). Then, we use a loop to enumerate the number j of bought hens, and the value of j ranges from 0 to 33 (because hens can buy up to 33).

After enumerating the number of purchased cocks and hens, we can calculate the number k of chickens purchased. Since the chicken is three yuan, we need to divide the remaining money by 3 to get the number k of chickens.

Finally, we also need to check whether the total number of purchased chickens is 100, and if so, output the purchase quantity of the current plan.

The following is the C language code implementation:

#include <stdio.h>

int main(

Guess you like

Origin blog.csdn.net/qq_39605374/article/details/132285562