gpt4 book ai didi

javascript - 如何返回数字 A 除以数字 B 的次数,直到它包含余数?

转载 作者:行者123 更新时间:2023-12-01 23:01:57 24 4
gpt4 key购买 nike

这里的上下文是我的问题:

我有一个零件表。一个零件是由一组 Material 制成的,我们称其为常量 Material 变量 z。所以这个表是一个由一组 Material 制成的零件表。此表中的每一行都是一个新部分。还有一列指示将从 1 套 Material 生产的零件数量。

如果我有:

var z = 1 //just some constant

var x = 5 //the amount of parts produced from a single set of materials

var y = 23 //the total amount of parts that have to be made

我知道用一组 Material 生产的零件数量,我也知道需要生产的零件数量。我生产的零件永远不会比需要的少,所以我知道 4 套 Material 可以生产 20 个零件,但我仍然少 3 个。如果我使用 5 套 Material ,我可以生产 25 个零件,剩下的 2 个零件。

我的问题是我尝试使用 mod 解决这个问题,但我在某处犯了错误

//Fun
const fun = async () => {
try {


let x = 23;
let y = 5;
const result = x/y

if(x%y == 0) {
console.log(x, ' divided by', y, ' has remainder of: ', x%y);
console.log(y, ' divided by', x, ' has remainder of: ', y%x);

}
else {
console.log(x, ' divided by', y, ' has remainder of: ', x%y);
console.log(y, ' divided by', x, ' has remainder of: ', y%x);
}

} catch (err) {
console.log(err.message);
}
}

因此,为了进一步增加清晰度,我总是想找到一个数可以除以某物的最大次数,如果它有余数,则记录余数。可能的解决方案是区分正余数还是负余数?感谢任何帮助,谢谢!!!

最佳答案

Math.floor( A/B ),其中 A 是所需数量,B 是一组中的件数,将为您提供余数之前的除法数(因为除法是只是可以从 A 中减去 B 的次数,然后我们使用 Math.floor 向下舍入),(A%B) 将给出余数。

关于javascript - 如何返回数字 A 除以数字 B 的次数,直到它包含余数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71607724/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com