php中用for巢狀迴圈解決雞兔同籠問題
阿新 • • 發佈:2018-12-11
上課老師佈置的任務
原始碼:
--------------------------------------------------------------------------------------------------------------------------------------------
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <?php $tou=40; for($chicken=1;$chicken<$tou;$chicken++) { for($rabbit=1;$rabbit<$tou;$rabbit++) { if(($rabbit+$chicken==$tou)&&(2*$chicken+4*$rabbit==100)) { echo "雞有:".$chicken."只"."<br>"; echo "兔有:".$rabbit."只"; break; } } } ?> </body> </html>