1 回答

TA貢獻(xiàn)1884條經(jīng)驗 獲得超4個贊
也許你正在尋找這樣的東西:
function getDistributedArray(n, max) {
var a = [];
var r = n; // rest of total sum
var c = Math.ceil(n / max); // get maximal number of elements in array
var i = 0; // index
while (r > 0) {
var t = Math.ceil(r / c); // get max number from the rest
a[i++] = t;
r -= t;
c--;
}
return a;
}
console.log(getDistributedArray(30, 10)); // [10, 10, 10]
console.log(getDistributedArray(32, 10)); // [8, 8, 8, 8]
console.log(getDistributedArray(34, 10)); // [9, 9, 8, 8]
console.log(getDistributedArray(99, 10)); // [10, 10,..., 9]
添加回答
舉報