公鸡5/只,母鸡3/只,小鸡2/只,100可以买100只鸡
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>JS之for循环实现"百钱买百鸡"</title>
    <link rel="stylesheet" href="">
    <script>
        for(var i=0;i<=20;i++){
            for(var j=0;j<=33;j++){
                for(var z=0;z<=100;z++){
                    if((i+j+z==100)&&(5*i+3*j+z/3==100)){
                        document.write('公鸡有'+i+"只;"+"母鸡有"+j+"只;"+"小鸡有"+z+"只"+"<br>");
                    }
                }
            }
        }
    </script>
</head>
<body>
</body>
</html>

两个for

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>JS之for循环实现"百钱买百鸡"</title>
    <link rel="stylesheet" href="">
    <script>
        for(var i=0;i<=20;i++){
            for(var j=0;j<=33;j++){
                z=100-i-j;
                    if((z%3==0)&&(5*i+3*j+z/3==100)){
                        document.write('公鸡有'+i+"只;"+"母鸡有"+j+"只;"+"小鸡有"+z+"只"+"<br>");
                    }
            }
        }
    </script>
</head>
<body>
</body>
</html>

标签:JavaScript

你的评论