Home > php教程 > php手册 > body text

百钱买百鸡问题解决 PHP

WBOY
Release: 2016-06-06 19:45:01
Original
2204 people have browsed it

我国古代数学家张丘建在《算经》一书中曾提出过著名的“百钱买百鸡”问题。 该问题叙述如下:鸡翁一,钱五;鸡母一,钱三;鸡雏三,钱一; 百钱买百鸡,则翁,母,雏各几何? ?php/** * $cock 鸡翁 $hen 母鸡 $chicken 小鸡 * 可得$cock + $hen + $chicken =

    我国古代数学家张丘建在《算经》一书中曾提出过著名的“百钱买百鸡”问题。

    该问题叙述如下:鸡翁一,值钱五;鸡母一,值钱三;鸡雏三,值钱一;

                    百钱买百鸡,则翁,母,雏各几何?

<?php /**
 * $cock 鸡翁  $hen 母鸡  $chicken  小鸡
 * 可得$cock + $hen + $chicken = 100
 *  百钱  5 * $cock + 3 * $hen + $chicken / 3 = 100 
 **/
for ($cock = 1; $cock < 20; $cock++) //$cock 鸡翁最多只有20只,因为有100只鸡,所以$cock < 20
{
    for ($hen = 1; $hen < 33; $hen++) //$hen 母鸡最多33只 $hen < 33
    {
        $chick = 100 - $cock - $hen;   //$chick 小鸡
        if ( ($cock + $hen + $chick) == 100 && (5 * $cock + 3 * $hen + $chick / 3) == 100)
        {
            echo 'The cock: ', $cock, "; The hen: ", $hen, "; The chicken: ", $chick, "<br />";
        }
    }
}
?>
Copy after login
特与大家分享。

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template