pingu,
let A = [15.8, 1.2, 1.1, 1.0, 0.9, 0.8, 0.7, 0.6, 0.6, 0.6, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5];
let K = 2;
let arr = Combination(A, K);
function Combination(A, K) {
A.sort((a, b) => a - b);
let local = [], arr = [], pr = [];
unique_combination(0, 0, K, local, A, pr, arr);
return arr
}
function unique_combination(l, sum, K, local, A, pr, arr) {
if (sum == K) {
pr = local;
//console.log("pr",pr);
arr.push(pr.slice(0));
}
for (let i = l; i < A.length; i++) {
if (sum + A[i] > K)
continue;
if (i > l && A[i] == A[i - 1])
continue;
local.push(A[i]);
unique_combination(i + 1, sum + A[i], K, local, A, pr, arr);
local.pop();
}
}
console.log("arr",arr);
|