1 回答
TA贡献1810条经验 获得超5个赞
var arr = [99.1, 92.2, 60, 50,
49.5, 45.7, 25.1, 20,
17.4, 13, 10, 7, 2.1, 2, 1
];
function combinations(array, n) {
var lists = [],
M = 1 << array.length;
for (var i = 1; i < M; ++i) {
var sublist = array.filter(function(c, k) {
return i >> k & 1
});
if (sublist.reduce(function(p, c) {
return p + c
}, 0) === n)
lists.push(sublist);
}
return lists;
}
console.log(combinations(arr, 100))
//[[60,20,13,7],[50,20,13,10,7],[49.5,25.1,17.4,7,1],[60,20,10,7,2,1],[49.5,20,17.4,10,2.1,1],[49.5,17.4,13,10,7,2.1,1]]
添加回答
举报