试试这个解决方案。对于此代码,我使用此参考github。
<script type="text/javascript">
var bol=false
var arr=[4, 6, 23, 10, 1, 3]
var maxx=Math.max.apply(Math, arr);
var index = arr.indexOf(maxx);
arr.splice(index, 1);
function k_combinations(set, k) {
var i, j, combs, head, tailcombs;
if (k > set.length || k <= 0) {
return [];
}
if (k == set.length) {
return [set];
}
if (k == 1) {
combs = [];
for (i = 0; i < set.length; i++) {
combs.push([set[i]]);
}
return combs;
}
combs = [];
for (i = 0; i < set.length - k + 1; i++) {
head = set.slice(i, i+1);
tailcombs = k_combinations(set.slice(i + 1), k - 1);
for (j = 0; j < tailcombs.length; j++) {
combs.push(head.concat(tailcombs[j]));
}
}
return combs;
}
function combinations(set) {
var k, i, combs, k_combs;
combs = [];
for (k = 1; k <= set.length; k++) {
k_combs = k_combinations(set, k);
for (i = 0; i < k_combs.length; i++) {
combs.push(k_combs[i]);
}
}
return combs;
}
var result=combinations(arr)
for(z=0;z<result.length;z++){
var tot=0
for(c=0;c<result[z].length;c++){
tot+=result[z][c]
}
if(tot===maxx){bol=true}
}
console.log(bol)
</script>
如果此解决方案不适合您的需求,我很抱歉让您浪费时间。