1 回答
TA贡献1821条经验 获得超4个赞
假设只能有一个填充盒,这应该可以解决问题。
function fill_space(space) {
const small_block = 21;
const big_block = 2 * small_block;
const min_filler = 7;
const max_filler = 26;
if (space % small_block == 0) {
const num_big = Math.trunc(space / big_block);
const num_small = (space - num_big * big_block) / small_block;
return { num_big: num_big, num_small: num_small, filler: 0 };
}
if (space < min_filler) {
return { num_big: 0, num_small: 0, filler: 0 };
}
const max_block_space = space - min_filler;
const num_big = Math.trunc(max_block_space / big_block);
const max_small_space = max_block_space - num_big * big_block;
const num_small = Math.trunc(max_small_space / small_block);
const filler = Math.min(
min_filler + max_small_space - num_small * small_block,
max_filler
);
return { num_big: num_big, num_small: num_small, filler: filler };
}
console.log(fill_space(0));
console.log(fill_space(6));
console.log(fill_space(21));
console.log(fill_space(42));
console.log(fill_space(48));
console.log(fill_space(50));
console.log(fill_space(63));
添加回答
举报