我正在计算某种化学,为此我需要组合总计等于N的值。我有以下数组,每个数组的值不能超过X
Array:
索引|最大值
0 | 370
1 | 185
2 | 740
3 | 277.5
4 | 277.5
5 | 1850
6 | 925
7 | 1850
我需要一个函数,该函数将从每个数组索引中计算出所有可能的值组合,总计为1850。
例如
0 | 77.00
1 | 700.00
2 | 50.00
3 | 300.00
4 | 700.00
5 | 15.00
6 | 7.00
7 | 1.00
总计= 1850
预先感谢您的帮助
答案 0 :(得分:1)
这是一种方法。
pointsLeft
)。将随机选取的数字添加到结果数组中的指定索引处(结果数组索引应遵循原始数组的顺序-而不是排序的顺序)pointsLeft
,则表示结果数组中的项等于所需的总和。所以我们只返回结果数组。
const testArray = [
370,
185,
740,
277.5,
277.5,
1850,
925,
1850,
]
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getCombination(values, pointsLeft) {
let sorted = values.map((x, i) => {
return [x, i]
}).sort((a, b) => a[0] - b[0]); // sorted from small -> large - keeping the original index
const result = new Array(values.length).fill(0)
while (pointsLeft > 0) {
for (const [_, [value, original_index]] of sorted.entries()) {
someRandomValueToSubtract = getRandomInt(1, value / 2)
if (pointsLeft - someRandomValueToSubtract < 0) continue
pointsLeft -= someRandomValueToSubtract
result[original_index] += someRandomValueToSubtract
}
}
return result
}
const x = getCombination(testArray, 1850)
console.log("ITEMS:", x)
console.log("SUM:", x.reduce(function(a, b) {
return a + b;
}, 0))
.as-console-wrapper { max-height: 100% !important; top: 0; }