Hello everyone, let me share with you the 100-Yuan-100-Chicken program to write Python. Many people still don’t know this. Let’s explain it in detail below. Now let’s take a look!
python case: buying chicken for a hundred dollars
Case directory
1. Problem description
- A cock is worth five cents;
- A hen is worth three pieces;
- Three chicks are worth a penny;
- Now I want to buy a hundred chickens with a hundred coins;
- How many roosters, hens and chicks are there?
2. Problem analysis
- If you only buy roosters with 100 yuan, you can buy up to 20 roosters;
- But the question requires the purchase of one hundred roosters, so it can be seen that the number of roosters purchased must be between 0 and 20;
- Similarly, the number of hens is between 0 and 33;
- Here, the numbers of roosters, hens and chicks are set to cock, hen and chicken respectively, cock+hen+chicken=100;
- Therefore, the problem of buying a hundred chickens for a hundred dollars is transformed into a problem of solving the indefinite equations cock + hen + chicken == 100 and 5xcock+3xhen+chicken/3=100.
3. Actual combat
1. Code
code show as below:
for cock in range(0, 20):
for hen in range(0, 30):
chicken = 100 - cock - hen
if chicken % 3 == 0:
if 5 * cock + 3 * hen + chicken / 3 == 100:
print('公鸡{}只,母鸡{}只,小鸡{}只'.format(cock, hen, chicken))
2. Operation results
The running results are as follows:
0 roosters, 25 hens, 75 chicks
4 roosters, 18 hens, 78 chicks
8 roosters, 11 hens, 81 chicks
12 roosters, 4 hens, chicks 84
4. One sentence a day
Yesterday's abyss, today's summary of basic knowledge points of python . Although the road is far away, we will get there soon. Although things are difficult, if you do it, you will succeed.