Home > Article > Backend Development > PHP Hundred Money Hundred Chicken Problem (Three Problem-solving Ideas and Answers)
A mathematical problem raised by Zhang Qiujian, an ancient Chinese mathematician, in his book Suan Jing: One hen is worth five, one hen is worth three, and three chicks are worth one. If you buy a hundred chickens for a hundred dollars, how much do the chickens, mothers, and chicks cost?
Translation is:
5 yuan for a rooster, 3 yuan for a hen, 3 yuan for a chick , I bought 100 chickens for 100 yuan, how many are each?
The following uses PHP to solve this problem in three ways:
Assumption:
If there is 1 rooster, 1 hen, and 1 chick, the total price is:..., incorrect.
If there is 1 rooster, 1 hen, and 2 chicks, the total price is:..., incorrect.
If there is 1 rooster, 1 hen, and 3 chicks, the total price is:..., incorrect.
........................
If there is 1 rooster and 2 hens, the little For 1 chicken, the total price is:..., incorrect.
If there is 1 rooster, 2 hens, and 2 chicks, the total price is:..., incorrect.
If there is 1 rooster, 2 hens, and 3 chicks, the total price is:..., incorrect.
........................
If there are 100 roosters and 100 hens , 100 chicks, then the total price is:..., incorrect.
This kind of programming idea is called "exhaustive", which means to list all possible answers and then verify them one by one.
The code is as follows:
<?php $count = 0; for($gongji = 0;$gongji <= 100;$gongji++){ for ($muji=0; $muji <= 100; $muji++) { for ($xiaoji=0; $xiaoji <= 100 ; $xiaoji++) { if($gongji + $muji + $xiaoji ==100 && $gongji*5 + $muji*3 + $xiaoji / 3 == 100){ echo "<br>公鸡:$gongji,母鸡: $muji,小鸡: $xiaoji"; } $count++; } } } echo "<br>".$count;
Result:
Rooster: 0, hen: 25, chick: 75 rooster : 4, hens: 18, chicks: 78 roosters: 8, hens: 11, chicks: 81 roosters: 12, hens: 4, chicks: 841030301
The code is as follows
$count = 0; for($gongji = 0;$gongji <= 100 / 5;$gongji++){ for ($muji=0; $muji <= 100 / 3; $muji++) { $xiaoji = 100 - $gongji - $muji; if($gongji*5 + $muji*3 +$xiaoji / 3 == 100){ echo "<br>公鸡:$gongji,母鸡: $muji,小鸡: $xiaoji"; } $count++; } } echo "<br>".$count;
Result:
Cock: 0, hen: 25, chick: 75 Roosters: 4, Hens: 18, Chicks: 78 Roosters: 8, Hens: 11, Chicks: 81 Roosters: 12, Hens: 4, Chicks: 84714
codes are as follows
$count = 0; for($gongji = 0;$gongji <= 100 / 5;$gongji++){ for ($muji=0; $muji <= (100-$gongji*5) / 3; $muji++) { $xiaoji = 100 - $gongji - $muji; if($gongji*5 + $muji*3 +$xiaoji / 3 == 100){ echo "<br>公鸡:$gongji,母鸡: $muji,小鸡: $xiaoji"; } $count++; } } echo "<br>".$count;
Result:
Rooster: 0, hen: 25, chick: 75 Rooster: 4 , hens: 18, chicks: 78 roosters: 8, hens: 11, chicks: 81 roosters: 12, hens: 4, chicks: 84364
Summary:
It is easy to think of using a triple for loop directly, but the complexity of the triple for loop is too great, so you should think of reducing the loop. Simply subtract the roosters and hens from the total number of chickens to reduce one layer of loops, which can reduce running time and improve code efficiency.
The above is the detailed content of PHP Hundred Money Hundred Chicken Problem (Three Problem-solving Ideas and Answers). For more information, please follow other related articles on the PHP Chinese website!