Question 100 money to buy 100 chicken with notes
Problem: write a program, calculate 100 money to buy 100 chicken problem. Suppose you have a rooster for $5, a hen for $3, and three chickens for $1. Now you have $100 and want to buy 100 chickens. How many ways can you buy them?
This is my first real programming problem as a beginner in Python, and I want to record my approach.
That was my initial idea
At first, I hoped to calculate the buying method by traversing the numbers of the three kinds of chickens and adding the judgment conditions of hundred money and hundred chickens in the cycle process. However, with the help of my friend, I found that z was not certain that z was a multiple of 3 for the number of chickens, so it could be known that this algorithm would calculate more situations.
This is the modified code
By adding a judgment condition on whether z is a multiple of 3, the overcalculated buy method can be reduced.
Then the second idea is that the number of cocks and hens can be determined based on the price
x< = 20
y< 34
So the number of times can be improved to the following figure in the loop
Finally, the calculation time of the two methods is compared.
The results show that the second method is faster than the first method, so the second method is chosen.
Writing a blog for the first time, I hope to correct my mistakes.