<> problem
A rooster costs five yuan , A hen costs three yuan , Three chicks for a dollar , Now I have to buy 100 chickens for 100 yuan , Ask the cock , hen , How many chickens each ? How many options are there in total ?
<> thinking
<> There are two conditions for questions :
1. cock X5 + hen X3 + chick /3 = 100
2. cock + hen + chick =100
So when writing a program, you need to meet these two conditions
<> Analysis scheme
Only buy cock , There are at most 20 schemes , There are at most thirty-three schemes to buy only hens , There are at most 100 options for buying chicks , But when writing a program, because it contains a head but not a tail , So when you write a program, you should add one more
<> Programming
<> Procedure I , Triple cycle solution
count = 0 for cock in range(21): for hen in range(35): for chick in range(101):
if cock + hen + chick == 100 and cock * 5 + hen * 3 + chick / 3 == 100: count =
count+ 1 print(' cock :{}\t hen :{}\t chick :{}\t'.format(cock,hen,chick)) print(
' Statistical results : A hundred dollars for a hundred chickens {} Schemes .'.format(count))
<> Running results
<> Procedure II , Double loop solution
Knowing the total number of chickens purchased , Know the number of cocks and hens , The number of chicks is naturally determined , So please see the following procedure
count = 0 for cock in range(21): for hen in range(35): chick = 100 - cock - hen
if cock + hen + chick == 100: count = count + 1 print(' cock :{}\t hen :{}\t chick :{}\t'.
format(cock,hen,chick)) print(' Statistical results : A hundred dollars for a hundred chickens {} Schemes .'.format(count))
<> Running results
The result is the same , And the second scheme is simpler and simpler
Technology