java 第一个数字小于第二个数字时的模除法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16311643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:36:46  来源:igfitidea点击:

Modulus division when first number is smaller than second number

javamodulointeger-divisionarithmetic-expressions

提问by Jessica M.

I apologize if this is a simple question but I'm having trouble grasping the concept of modulus division when the first number is smaller than the second number. For example when 1 % 4 my book says the remainder is 1. I don't understand how 1 is the remainder of 1 % 4.
1 / 4 is 0.25. Am I thinking about modulus division incorrectly?

如果这是一个简单的问题,我深表歉意,但是当第一个数字小于第二个数字时,我无法理解模数除法的概念。例如,当 1 % 4 我的书说余数是 1 时。我不明白 1 是 1 % 4 的余数
。1 / 4 是 0.25。我是否错误地考虑了模数除法?

采纳答案by misberner

First, in Java, % is the remainder (not modulo) operator, which has slightly different semantics. That said, you need to think in terms of integer-only division, as if there were no fractional values. Think of it as storing items that cannot be divided: you can store zero items of size 4 in a storage of overall capacity one. Your remaining capacity after storing the maximum number of items is one. Similarly, 13%5 is 3, as you can fit 2 complete items of size 5 in a storage of size 13, and the remaining capacity is 13 - 2*5 = 3.

首先,在 Java 中,% 是余数(不是模)运算符,其语义略有不同。也就是说,您需要考虑仅整数除法,就好像没有小数值一样。可以将其视为存储无法分割的项目:您可以将大小为 4 的零个项目存储在总容量为 1 的存储中。存储最大项目数后的剩余容量为 1。同样,13%5 是 3,因为您可以将 2 个大小为 5 的完整项目放入大小为 13 的存储中,剩余容量为13 - 2*5 = 3

回答by Jean-Bernard Pellerin

If you divide 1 by 4, you get 0 with a remainder of 1. That's all the modulus is, the remainder after division.

如果将 1 除以 4,则得到 0,余数为 1。这就是模数的全部内容,即除法后的余数。

回答by Jorge Casariego

I am going to add a more practical example to what "Jean-Bernard Pellerin" already said.

我将在“Jean-Bernard Pellerin”已经说过的内容中添加一个更实际的例子。

It is correct that if you divide 1 by 4 you get 0 but, Why when you do 1 % 4 you have 1 as result?

如果将 1 除以 4 得到 0 是正确的,但是为什么当你做 1 % 4 时结果是 1?

Basically it is because this:

基本上是因为这个:

n = a / b (integer), and
m = a % b = a - ( b * n )

So,

所以,

 a    b    n = a/b  b * n  m = a%b
 1    4      0        0      1    
 2    4      0        0      2
 3    4      0        0      3
 4    4      1        0      0
 5    4      1        4      1

Conclusion: While a < b, the result of a % b will be "a"

结论:当 a < b 时,a % b 的结果将是“a”

回答by sashkello

Another way to think of it as a representation of your number in multiples of another number. I.e, a = n*b + r, where b>r>=0. In this sense your case gives 1 = 0*4 + 1. (edit: talking about positive numbers only)

另一种方式将其视为您的数字以另一个数字的倍数表示。即a = n*b + r,在哪里b>r>=0。从这个意义上说,您的情况给出了1 = 0*4 + 1. (编辑:只谈论正数)

回答by Ravi Trivedi

I think you are confused between %(Remainder)and /(Division)operators.

我认为您在%(Remainder)/(Division)运营商之间感到困惑。

When you say %, you need to keep dividing the dividend until you get the remainder 0 or possible end. And what you get in the end is called Remainder.

当你说 时%,你需要不断地除以红利,直到得到余数 0 或可能的结束。而你最终得到的叫做Remainder.

When you say /, you divide the dividend until the divisor becomes 1. And the end product you get is called Quotient

当你说 时/,你把红利除以除数变成 1。你得到的最终产品叫做Quotient

回答by XcodeNOOB

Another nice method to clear things up, In modulus, if the first number is > the second number, subtract the second number from the first until the first number is less than the second.

另一种解决问题的好方法,在模数中,如果第一个数字 > 第二个数字,则从第一个数字中减去第二个数字,直到第一个数字小于第二个数字。

17 % 5 = ?
17 - 5 = 12
12 % 5 = ?
12 - 5 = 7
7 % 5 = ?
7 - 5 = 2
2 % 5  = 2

Therefore 17 % 5, 12 % 5, 7 % 5 all give the answer of 2. This is because 2 / 5 = 0 (when working with integers) with 2 as a remainder.

因此 17 % 5, 12 % 5, 7 % 5 都给出了 2 的答案。这是因为 2 / 5 = 0(使用整数时),余数为 2。