整数除法的Java余数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32778671/
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-08-11 13:04:57  来源:igfitidea点击:

Java Remainder of Integer Divison?

javamathdivide

提问by DisasterCoder

I was searching around about this topic but I still don't get it, if someone can elaborate I would be very thankful.

我正在搜索这个主题,但我仍然不明白,如果有人能详细说明,我将非常感激。

My task is to divide two variables as integer division with remainder.. The problem is, that I have no clue what a remainder is, for now I did something like that, this is what I found by searching through the internet:

我的任务是将两个变量作为整数除法与余数相除。 问题是,我不知道余数是什么,现在我做了类似的事情,这是我通过互联网搜索发现的:

int a;
int b;
int remainder = Math.Pow(a,2) % b;

System.out.println("a^2 / b = " + Math.Pow(a,2) / b);
System.out.println("remainder = " + remainder);

if I for example set (a = 43) and (b = 67)

例如,如果我设置 (a = 43) 和 (b = 67)

Then I will get this reslut:

然后我会得到这个结果:

a^2 / b = 27
remainder = 40

Now since I have no idea what the remainder is (this is just a suggestion form the internet) I have no idea if this is the correct answer..?

现在因为我不知道剩下的是什么(这只是来自互联网的建议)我不知道这是否是正确答案..?

Thanks for any help,

谢谢你的帮助,

Kind Regards

亲切的问候

采纳答案by SomeJavaGuy

If you are looking for the mathematical modulo operation you could use

如果您正在寻找可以使用的数学模运算

int x = -22;
int y = 24;
System.out.println(Math.floorMod(x, y));

If you are not interested in the mathematical modulo (just the remainder) then you could use

如果您对数学模数(只是余数)不感兴趣,那么您可以使用

int x = -22;
int y = 24;
System.out.println(x%y);

回答by hd1

int remainder = a % b;will sort you. The remainder operator returns the remainder of the division.

int remainder = a % b;会给你排序。余数运算符返回除法的余数。



Note that the remainder operator is also called the modulo operator. However, this is incorrect for Java as Java will return a negative value if the left operand ais negative.

请注意,余数运算符也称为模运算符。然而,这对 Java 来说是不正确的,因为如果左操作数a为负,Java 将返回一个负值。

回答by Jo?o Neves

Yes, the %operator will return the remainder of the Integer division.

是的,%运算符将返回整数除法的余数。

To know more about the remainder of the Integer division check out Wikipedia:

要了解有关整数除法其余部分的更多信息,请查看维基百科

If a and d are integers, with d non-zero, it can be proven that there exist unique integers q and r, such that a = qd + r and 0 ≤ r < |d|. The number q is called the quotient, while r is called the remainder.

如果 a 和 d 是整数,其中 d 非零,则可以证明存在唯一整数 q 和 r,使得 a = qd + r 且 0 ≤ r < |d|。数 q 称为商,而 r 称为余数。

回答by Taras Melnyk

    public static void main(String[] args) {
        int dividend = 139, divisor = 7;

        int quotient = dividend / divisor;
        int remainder = dividend % divisor;

        System.out.println("The Quotient is = " + quotient);
        System.out.println("The Remainder is = " + remainder);
    }

Output:

输出:

The Quotient is = 19

The Remainder is = 6

商是 = 19

余数是 = 6

回答by Abdel-Raouf

%operator will return the remainder of the Integer division.

%运算符将返回整数除法的余数。

What modules actually does under the hood ?

什么模块在引擎盖下实际上做了什么?

Modules tend to remove cyclesfrom the number, until it reaches a positive number that is smaller than the number of the cycle which we call modulo ORa negative number which we call a reminder.

模块倾向于cycles从数字中删除,直到它达到一个小于我们称为模数的循环数OR的正数,我们称之为 a 的负数reminder

However, Using %operator is time expensive.

但是,使用%运算符很费时间。

To avoid using %while getting the same result, we can use the following:

为了避免使用 %而获得相同的结果,我们可以使用以下内容:

  • While(a >= n) a -= n;(when ais a positive number)
  • While(a < 0) a += n;(when ais a negative number)

  • a = n*q + rthat means r = a - n*qWhile q is the integer division of a/nwhich means a%n == a - n * Math.toIntExact(a/n)Which is sufficient when ais a positive number.

  • While ais a negative number, we can use (a%n + n) % nWhich will give you module.
  • While(a >= n) a -= n;(whena是正数)
  • While(a < 0) a += n;(whena是负数)

  • a = n*q + r这意味着r = a - n*qWhileq is the integer division of a/n这意味着a%n == a - n * Math.toIntExact(a/n)which is enough whena是正数。

  • 虽然a是负数,但我们可以使用(a%n + n) % nwhich 会给你模块。

Case Scenario on Clock:

时钟案例场景:

if it is now 9 O'clock, what time after 4 hours =>9+4 = 13h =>13%12=1 while 12 is the cycle number in the clock

如果现在是 9 点,那么 4 小时后什么时候=>9+4 = 13h =>13%12=1while 12 is the cycle number in the clock

What if we need to calculate time before 24hours (Yesterday) from now which is 9 O'clock, then: 24(2*12)=>Yesterday Means 9-24 = -15hWhile the right answer is 9, to solve this we will use (a%n + n) % nWhile a%n == (a - n * Math.toIntExact(a/n))then -15 - 12 * Math.toIntExact(-15/12) = -3=> -3 + 12 = 9=> 9%12=> 9 - 12 * Math.toIntExact(9/12) = 9Which is the right answer.

如果我们需要24从现在开始计算小时(昨天)之前的时间9 O'clock,那么: 24(2*12)=>昨天意味着9-24 = -15h虽然正确答案是9,但为了解决这个问题,我们将使用(a%n + n) % nWhile a%n == (a - n * Math.toIntExact(a/n))then -15 - 12 * Math.toIntExact(-15/12) = -3=> -3 + 12 = 9=> 9%12=>9 - 12 * Math.toIntExact(9/12) = 9哪个是正确答案。

This is the code for the clock Scenario:

这是时钟场景的代码:

public static void main(String args[]){
    Scanner scanner = new Scanner(System.in);
    int a = scanner.nextInt(); // a = -15
    int n = scanner.nextInt(); // cycle = 12

    int reminder = a - (n * Math.toIntExact(a / n));
    int reminder_plus_n = (reminder + n);
    int modulo = reminder_plus_n - (n * Math.toIntExact(reminder_plus_n / n));
    System.out.println(modulo); // Answer = 9
}