js——猴子选大王

 

题目:

 

解题思路:

 

 

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>猴子选大王</title>
</head>
<body>

    <script type="text/javascript">
        var total = prompt("请输入猴子个数:");
        var kick = prompt("请输入踢出第几只猴子的序号:");
        var monkey = [];
        for(var i=0;i<total;i++ ){
            monkey.push(i); // 形成猴子群
        }
        i=0;
        while(monkey.length>1){
            ++i;
            var head = monkey.shift();//开头取猴子
            if(i%kick!=0){
                monkey.push(head);
            }
        }
        console.log(("编号"+(monkey[0]+1)+"是猴王"))
    </script>


</body>
</html>

 

运行结果:

 

posted @ 2020-11-30 11:56  ZengZG  Views(1527)  Comments(0Edit  收藏  举报