X
。X
数的正整数"components“(D6
)。C1+C2+...+Cn = X
一样将这些组件添加到一起。N
作为组件数量的限制。B
作为最大分量的极限[8;8;5]
,那么组件的avg是7
,分发是abs(8-7)+abs(8-7)+abs(5-7) = 4
。dist([8;8;5])=4 dist([7;7;7])=0
->最低分布是[7;7;7]
。示例
X = 17
N = 3
B = 8
[8;8;1]
、[7;7;3]
和[6;6;5]
。[6;6;5]
中,这就是我们所需要的。X = 21
N = 3
B = 8
[8;8;5]
和[7;7;7]
。[7;7;7]
。X = 22
N = 3
B = 8
[8;8;6]
和没有更多。[8;8;6]
X = 25
N = 3
B = 8
我正在寻找用Javascript编写的这个问题的最短脚本。我有一个10线的解决方案。
我的解决方案:
const findComponents = (X, B, N) => {
let S = null;
let mainComponent = Math.ceil(X / N);
if (mainComponent <= B){
let otherComponent = X % mainComponent;
S = Array(N - 1).fill(mainComponent);
S.push(otherComponent == 0 ? mainComponent : otherComponent);
}
return S;
}
发布于 2022-08-09 19:01:53
发布于 2022-08-10 07:10:59
ÅœIùʒ@P}ʒ¦Ë}θ
输入与挑战描述相同的顺序:X,N,B
。
解释:
Ŝ # Get all lists of positive integers that sum to the first (implicit) input `X`
Iù # Only keep the lists with a length equal to the second input `N`
ʒ # Filter this list by:
@ # Check if the third (implicit) input `B` is >= the current value
P # Check that it's truthy for all values in the list
}ʒ # After the filter: filter again:
¦ # Remove the first value of the list
Ë # Check if all other values are the same
}θ # After the filter: pop and keep the last list
# (which is output implicitly as result)
https://codegolf.stackexchange.com/questions/250839
复制相似问题