从成员数为N的集合S中,选出M个元素,分别求其排列与组合结果集,即 A(N, M)与C(N, M)
function queue(arr, size){ if(size > arr.length){ return;} var allResult = []; (function(arr, size, result){ if(result.length == size){ allResult.push(result); }else{ for(var i =0 ,len = arr.length;i < len; i++){ var newArr = [].concat(arr), curItem = newArr.splice(i,1); arguments.callee(newArr, size, [].concat(result,curItem)); } } })(arr, size, []); return allResult; } function choose(arr, size ){ var allResult = []; (function(arr, size, result){ var arrLen = arr.length; if(size > arrLen){ return; } if(size == arrLen){ allResult.push([].concat(result, arr)) }else{ for(var i =0 ; i < arrLen; i++){ var newResult = [].concat(result); newResult.push(arr[i]); if(size == 1){ allResult.push(newResult); }else{ var newArr = [].concat(arr); newArr.splice(0, i + 1); arguments.callee(newArr, size - 1, newResult); } } } })(arr, size, []); return allResult; } function showResult(result){ console.log('The number of result sets: ' + result.length ); for(var i=0 , len = result.length;i < len; i++){ console.log(result[i]); } } var arr = ['姬光','王子', '三桂','科长']; showResult(choose(arr, 4)); showResult(queue(arr, 4));