<?php
/*
Suppose someone has 100000 in cash. A fee is required for each intersection. The payment rule is to pay 5% when he has more than 50000 cash, if the cash is less than or equal to 50000 per 5000. Please write a procedure to calculate how many times this person can pass the junction.
*/
$money = 100000;
for ($i =0; $money >=5000; $i + +) {
if ($money >=50000) {
$money = $money-$money *0.05;
}else if ($money >=5000) {
$money-= 5000;
}
}
Echo ' This guy can go ', $i, ' times<br/> ';
/*
hundred dollars to buy hundred chickens
A 5-piece rooster;
A hen is 3 dollars;
Chicken three only 1 pieces.
Now 100 bucks bought 100 chickens, and asked the 100 chickens, how many cocks, hens, chickens?
*/
for ($g =0; $g <20; $g + +) {
for ($m =0; $m <34; $m + +) {
for ($x =0; $x <100; $x + +) {
if ($g + $m + $x ==100 && $g *5+ $m *3+ $x *1/3==100) {
Echo ' Gongji: ', $g, ', ' MuJi: ', $m, ', ' Xiaoji: ', $x, ' <br/> ';
}
}
}
}
/* Optimize code */
for ($g =0; $g <20; $g + +) {
for ($m =0; $m <34; $m + +) {
$x =100-$g-$m;
if ($g *5+ $m *3+ $x/3==100) {
Echo ' Gongji: ', $g, ', ' MuJi: ', $m, ', ' Xiaoji: ', $x, ' <br/> ';
}
}
}
?>
PHP algorithm Exercises