查找数组中与后面项匹配的和猴子选大王

查找数组中与后面项匹配的(返回的是数组里面的,而不是外面的!!!)

function destroyer(...rest){//变为数组的形式传入
    //rest.shift()//删除数组第一项,返回值被删除的那一项,原有数组改变
    return rest.shift().filter(function(v){//筛选和判断一个数组是否包含一个指定的值
        //return rest.includes(v);//找里面和外面都有的[1,2,6]
        return !rest.includes(v);//找里面有外面没有的[5,99]
    })
}
console.log(destroyer([1,2,6,5,99],1,2,3,4,6,13,88))


function destroyer(...rest){
return rest.shift().filter(v => !rest.includes(v))
}

 

 //假设有N个猴子围城一圈,从1开始报数,数到M的猴子退出。最后剩下的猴子是大王。求大王的最初位置

function circle(n,m){
    let monkeys = [...Array(n).keys()].map(item => item+1);
    let index = 0;
    while(monkeys.length > 1){
        index = (index + m -1) % monkeys.length;
        //console.log(`得到${monkeys[index]}`);
        monkeys.splice(index,1)
    }
    return monkeys
}
console.log(`结果${circle(121,7)}`)

//假设有N个猴子围城一圈,从1开始报数,数到M的猴子退出。第二轮,倒着数,数到M的退出,第三轮正数。。。以此类推。最后剩下的猴子是大王。求大王的最初位置

function circle1(n,m){
    let monkeys = [...Array(n).keys()].map(item => item+1);
    while(monkeys.length > 1){
        let index = (m - 1) % monkeys.length;
        //console.log(`得到${monkeys[index]}`);
        monkeys.splice(index,1)
        monkeys.reverse();
    }
    return monkeys
}
console.log(`结果${circle1(121,7)}`)

 

posted @ 2020-05-14 16:41  石头记1  阅读(197)  评论(0编辑  收藏  举报