几个人换礼物,且分到的不能是自己的(方法2)
作者:互联网
//根据索引分礼物
var personNum = [1,2,3,4,5];
var presentNum = [1,2,3,4,5];
var result = [];
function getPres2(personNum,presentNum){
if(personNum.length==2){
result.push([personNum[0],presentNum[1]]);
result.push([personNum[1],presentNum[0]]);
console.log(result);
return result;
}
var rad = Math.floor(Math.random()*(personNum.length-1));
var rad2 = Math.floor(Math.random()*(presentNum.length-1));
if(personNum[rad]==presentNum[rad2]){
return getPres2(personNum,presentNum);
}
result.push([personNum[rad],presentNum[rad2]]);
personNum.splice(rad,1);
presentNum.splice(rad2,1);
return getPres2(personNum,presentNum);
}
getPres2(personNum,presentNum);
标签:分到,rad,presentNum,人换,var,result,personNum,Math,礼物 来源: https://www.cnblogs.com/xinyouhunran/p/14923448.html