since you said try your best, try your best.
function foo(sum, length) {
let result = Array.from({ length }).fill(parseInt(sum / length));
for (let i = 0; i < sum % length; iPP) {
result[parseInt(Math.random() * length)]PP;
}
return result;
}
console.log(foo(9, 4))
and if you don't try, that's it.
function foo(sum, length) {
let av = parseInt(sum / length);
let result = Array.from({ length }).fill(av);
function randomIndex(av) {
let index = parseInt(Math.random() * length);
return result[index] === av ? index : randomIndex(av);
}
for (let i = 0; i < sum % length; iPP) {
result[randomIndex(av)]PP;
}
return result;
}
console.log(foo(134, 50))
function getArray(m, n) {
let finalArr = [];
if(m <= n) {
for (let i = 0; i<n; iPP) {
if(i < m) {
finalArr.push(1);
} else {
finalArr.push(0);
}
}
} else {
let element = Math.ceil(m/n);
for (let j = 0; j < n; jPP) {
if(j < n - 1) {
finalArr.push(element);
} else {
finalArr.push(m-element*(n-1));
}
}
}
return finalArr.sort(function(a, b){
return Math.random() - Math.random();
});;
}
console.log(getArray(11, 3)); // [4, 3, 4]
console.log(getArray(9, 4)); // [2, 2, 3, 2]
console.log(getArray(3, 4)); // [1, 0, 1, 1]
function foo(m,n){
var i = parseInt(m/n)
var j = m % n
var arr = new Array(n).fill(i)
arr[n-1] = arr[n-1]+j
return arr
}
var d= foo(9,4)
console.log(d)
function foo(m,n){
var i = parseInt(m/n)
var j = m % n
console.log(j)
var arr = new Array(n).fill(i)
for(var d=0;d<j;dPP){
arr[d]=arr[d]+1
}
return arr
}
var d= foo(11,3)
console.log(d)
function run(m, n) {
const d = {},
v = parseInt(m / n),
result = new Array(n).fill(v);
let _v = m % n;
while (!!_v) {
const i = Math.floor(Math.random() * n);
if (!d[i]) {
result[i] += 1;
d[i] = true;
_v -= 1;
}
}
return result;
}
each array element value is as equal as possible
this can be expressed as "minimizing the standard deviation", that is,
gives two numbers m and n, finds an array array, with the smallest standard deviation and satisfies len (array) = = m
, and sum (array) = = n
.
just find the array and randomly disrupt the order of the elements.
< hr >
I think many students have answered the question. I prefer the answer @ papersnake .
function foo(m,n){
var i = parseInt(m/n)
var j = m % n
console.log(j)
var arr = new Array(n).fill(i)
for(var d=0;d<j;dPP){
arr[d]=arr[d]+1
}
return arr
}
var d= foo(11,3)
console.log(d)
of course, he doesn't do random operations here.
function fn(m, n) {
let i = m % n;
const result = new Array(n - i).fill(Math.floor(m/n));
while(i > 0) {
result.splice(Math.random()*result.length, 0, Math.ceil(m/n));
i--;
}
return result;
}
you can also try this method
function getArray(sum,length){
var av = parseInt(sum / length);
var left = sum % length;
var leftIndex = Math.abs(left);
var array = [];
var step = left > 0 ? 1 : -1;
for(var i = 0;i < length;iPP){
array.push(av);
}
for(var i = 0;i < leftIndex;){
var pos = parseInt(Math.random() * length);
var value = array[pos];
if(value === av){
array[pos] = value + step;
iPP;
}
}
return array;
}
console.log(getArray(0,3));//0,0,0
console.log(getArray(-11,3));// -4,-4,-3
console.log(getArray(-11,4));//-3,-3,-2,-3
console.log(getArray(11,3));//4,4,3
console.log(getArray(16,4));//4,4,4,4
function exp(sum,len){
let arr = new Array(len).fill(parseInt(sum/len))
let arr2 = []
for (let i=0;i<len;iPP){
arr2.push(i)
}
let len2 = sum%len
for (let i=0;i<len2;iPP){
arr[arr2.splice(Math.floor(Math.random()*arr2.length),1)[0]]PP
}
return arr;
}
a=(m,n)=>Array(n).fill(m/n|0).map((v,w)=>w<m%n?v+1:v)