PHP classic question: Hundred Money and Hundred Chicken Problem (Exhaustive Algorithm)_PHP Tutorial

WBOY
Release: 2016-07-12 09:04:48
Original
2757 people have browsed it

PHP classic question: Hundred Money and Hundred Chicken Problem (Exhaustive Algorithm)

Hundred dollars and hundred chickens problem:
Known: roosters are 5 yuan each, hens are 3 yuan each, and chicks are 3 yuan each
I bought 100 chickens with 100 yuan. Question: How many roosters, hens and chicks are there?
--Please consider the most efficient method possible
Things:
If there are 0 roosters, 0 hens, and 1 chick, is the number 100? Is the price 100? No
If there are 0 roosters, 0 hens, and 2 chicks, is the number 100? Is the price 100? No
If there are 0 roosters, 0 hens, and 3 chicks, is the number 100? Is the price 100? No
......
If there are 0 roosters, 0 hens, and 100 chicks, is the number 100? Is the price 100? No
If there are 0 roosters, 1 hen, and 1 chick, is the number 100? Is the price 100? No
If there are 0 roosters, 1 hen, and 2 chicks, is the number 100? Is the price 100? No
......
If there are 0 roosters, 1 hen, and 100 chicks, is the number 100? Is the price 100? No
If there are 0 roosters, 2 hens, and 1 chick, is the number 100? Is the price 100? No
......
If there are 100 roosters, 100 hens, and 0 chicks, is the number 100? Is the price 100? No
If there are 100 roosters, 100 hens, and 1 chick, is the quantity 100? Is the price 100? No
If there are 100 roosters, 100 hens, and 2 chicks, is the quantity 100? Is the price 100? No
......
This is called: exhaustive thinking (that is, testing all possible situations one by one)
PHP code:
echo "

Original idea:

";
$count = 0;
for($gongji = 0; $gongji <= 100; $gongji) {
for($muji = 0; $muji <= 100; $muji) {
for($xiaoji = 0; $xiaoji <= 100; $xiaoji) {
if($gongji*5 $muji*3 $xiaoji/3 == 100 && $gongji $muji $xiaoji == 100) {
echo "
There are $gongji roosters; $muji hens; and $xiaoji chicks;";
}
$count ; //Count times
}
}
}
echo "
Number of times: $count";
echo '
';
echo "

Code Optimization 1:

";
$count = 0;
for($gongji = 0; $gongji <= 100; $gongji) {
for($muji = 0; $muji <= 100; $muji) {
$xiaoji = 100 - $gongji - $muji;
if($gongji*5 $muji*3 $xiaoji/3 == 100) {
echo "
There are $gongji roosters; $muji hens; and $xiaoji chicks;";
}
$count ; //Count times
}
}
echo "
Number of times: $count";
echo '
';
echo "

Code Optimization 2:

";
$count = 0;
for($gongji = 0; $gongji <= 100/5; $gongji) { //Based on the total price: there are at most 100/5 roosters
for($muji = 0; $muji <= 100/3; $muji) { //Based on the total price: the maximum number of hens is 100/3
$xiaoji = 100 - $gongji - $muji;
if($gongji*5 $muji*3 $xiaoji/3 == 100) {
echo "
There are $gongji roosters; $muji hens; and $xiaoji chicks;";
}
$count ; //Count times
}
}
echo "
Number of times: $count";
echo '
';
echo "

Code optimization three:

";
$count = 0;
for($gongji = 0; $gongji <= 100/5; $gongji) { //Based on the total price: there are at most 100/5 roosters
for($muji = 0; $muji <= (100-$gongji*5)/3; $muji) { //Based on the total price and the money spent on the rooster: then the hen has at most (100- $gongji*5)/3 pieces
$xiaoji = 100 - $gongji - $muji;
if($gongji*5 $muji*3 $xiaoji/3 == 100) {
echo "
There are $gongji roosters; $muji hens; and $xiaoji chicks;";
}
$count ; //Count times
}
}
echo "
Number of times: $count";
echo '
';
echo "

Code optimization four:

";
$count = 0;
for($gongji = 0; $gongji <= 100/5; $gongji) { //Based on the total price: there are at most 100/5 roosters
for($muji = 0; $muji <= (100-$gongji*5)/3; $muji) { //Based on the total price and the money spent on the rooster: then the hen has at most (100- $gongji*5)/3 pieces
$xiaoji = 100 - $gongji - $muji;
if($xiaoji % 3 != 0) {continue;} //Considering the price of chickens, the number of chickens can only be reasonable if it is divisible by 3
if($gongji*5 $muji*3 $xiaoji/3 == 100) {
echo "
There are $gongji roosters; $muji hens; and $xiaoji chicks;";
}
$count ; //Count times
}
}
echo "
Number of times: $count";
Output results and calculation times:
Original idea:
There are 0 roosters; 25 hens; 75 chicks;
There are 4 roosters; 18 hens; 78 chicks;
There are 8 roosters; 11 hens; 81 chicks;
There are 12 roosters; 4 hens; 84 chicks;
Number of times: 1030301
Code Optimization 1:
There are 0 roosters; 25 hens; 75 chicks;
There are 4 roosters; 18 hens; 78 chicks;
There are 8 roosters; 11 hens; 81 chicks;
There are 12 roosters; 4 hens; 84 chicks;
Number of times: 10201
Code Optimization 2:
There are 0 roosters; 25 hens; 75 chicks;
There are 4 roosters; 18 hens; 78 chicks;
There are 8 roosters; 11 hens; 81 chicks;
There are 12 roosters; 4 hens; 84 chicks;
Number of times: 714
Code optimization three:
There are 0 roosters; 25 hens; 75 chicks;
There are 4 roosters; 18 hens; 78 chicks;
There are 8 roosters; 11 hens; 81 chicks;
There are 12 roosters; 4 hens; 84 chicks;
Number: 364
Code optimization four:
There are 0 roosters; 25 hens; 75 chicks;
There are 4 roosters; 18 hens; 78 chicks;
There are 8 roosters; 11 hens; 81 chicks;
There are 12 roosters; 4 hens; 84 chicks;
Number of times: 121

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1071710.htmlTechArticlePHP classic question: Hundred Money and Hundred Chicken Problem (Exhaustive Algorithm) Hundred Money and Hundred Chicken Problem: Known: Rooster 5 yuan each, hens 3 yuan each, 3 chicks 1 yuan. I bought 100 chickens with 100 yuan. Question: Rooster...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template