PHP algorithm Exercises

Source: Internet
Author: User

<?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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.