Java整数除法的余数?
Java Remainder of Integer Divison?
我正在搜索这个主题,但我仍然没有明白,如果有人能详细说明,我将不胜感激。
我的任务是将两个变量除以整数除法并取余..
问题是,我不知道余数是多少,现在我做了类似的事情,这是我通过互联网搜索发现的:
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);
如果我设置 (a = 43) 和 (b = 67)
那我会得到这个结果:
a^2 / b = 27
remainder = 40
现在因为我不知道余数是多少(这只是来自互联网的建议)我不知道这是否是正确答案..?
感谢您的帮助,
亲切的问候
int remainder = a % b;
会给你排序。余数运算符 return 是除法的余数。
请注意,余数运算符也称为模运算符。但是,这对于 Java 是不正确的,因为如果左操作数 a
为负,Java 将 return 为负值。
是的,%
运算符将 return 整数除法的余数。
要了解有关整数除法余数的更多信息,请查看 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.
如果您正在寻找可以使用的数学模运算
int x = -22;
int y = 24;
System.out.println(Math.floorMod(x, y));
如果您对数学模数(只是余数)不感兴趣,那么您可以使用
int x = -22;
int y = 24;
System.out.println(x%y);
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);
}
输出:
The Quotient is = 19
The Remainder is = 6
%
运算符将 return 整数除法的余数。
What modules actually does under the hood ?
模块倾向于从数字中删除 cycles
,直到它达到一个正数,该正数小于我们称之为模数的循环数 OR
一个负数,我们称之为 reminder
.
但是,使用 %
运算符非常耗时。
To avoid using %
while getting the same result, we can use the following:
While(a >= n) a -= n;
(当a
为正数时)
While(a < 0) a += n;
(当a
为负数时)
a = n*q + r
表示 r = a - n*q
而 q is the integer division of a/n
表示 a%n == a - n * Math.toIntExact(a/n)
当 a
为正数时就足够了。
- 虽然
a
是负数,我们可以用(a%n + n) % n
给你模数。
Case Scenario on Clock:
如果现在是9点,4小时后是几点=>
9+4 = 13h =>
13%12=1 while 12 is the cycle number in the clock
如果我们需要计算从现在开始 24
小时(昨天)之前的时间,即 9 O'clock
,那么:
24(2*12)
=>
昨天意味着 9-24 = -15h
虽然正确答案是 9
,为了解决这个问题,我们将使用 (a%n + n) % n
而 a%n == (a - n * Math.toIntExact(a/n))
然后 -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
}
您可以使用商和余数来计算计数和余数。
这是我为 Minecraft 插件编写的一些代码。
public class Division {
public static ModuloDivResult divideMod(double a, double b) {
double remainder = a % b;
return new ModuloDivResult((a / b) - (remainder / b), remainder);
}
}
我们还需要一个容器来容纳这两个值。
public class ModuloDivResult {
private double remainder;
private double count;
public ModuloDivResult(double count, double remainder) {
this.remainder = remainder;
this.count = count;
}
public double getRemainder() {
return remainder;
}
public double getCount() {
return count;
}
}
我正在搜索这个主题,但我仍然没有明白,如果有人能详细说明,我将不胜感激。
我的任务是将两个变量除以整数除法并取余.. 问题是,我不知道余数是多少,现在我做了类似的事情,这是我通过互联网搜索发现的:
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);
如果我设置 (a = 43) 和 (b = 67)
那我会得到这个结果:
a^2 / b = 27
remainder = 40
现在因为我不知道余数是多少(这只是来自互联网的建议)我不知道这是否是正确答案..?
感谢您的帮助,
亲切的问候
int remainder = a % b;
会给你排序。余数运算符 return 是除法的余数。
请注意,余数运算符也称为模运算符。但是,这对于 Java 是不正确的,因为如果左操作数 a
为负,Java 将 return 为负值。
是的,%
运算符将 return 整数除法的余数。
要了解有关整数除法余数的更多信息,请查看 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.
如果您正在寻找可以使用的数学模运算
int x = -22;
int y = 24;
System.out.println(Math.floorMod(x, y));
如果您对数学模数(只是余数)不感兴趣,那么您可以使用
int x = -22;
int y = 24;
System.out.println(x%y);
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);
}
输出:
The Quotient is = 19
The Remainder is = 6
%
运算符将 return 整数除法的余数。
What modules actually does under the hood ?
模块倾向于从数字中删除 cycles
,直到它达到一个正数,该正数小于我们称之为模数的循环数 OR
一个负数,我们称之为 reminder
.
但是,使用 %
运算符非常耗时。
To avoid using
%
while getting the same result, we can use the following:
While(a >= n) a -= n;
(当a
为正数时)While(a < 0) a += n;
(当a
为负数时)a = n*q + r
表示r = a - n*q
而q is the integer division of a/n
表示a%n == a - n * Math.toIntExact(a/n)
当a
为正数时就足够了。- 虽然
a
是负数,我们可以用(a%n + n) % n
给你模数。
Case Scenario on Clock:
如果现在是9点,4小时后是几点=>
9+4 = 13h =>
13%12=1 while 12 is the cycle number in the clock
如果我们需要计算从现在开始 24
小时(昨天)之前的时间,即 9 O'clock
,那么:
24(2*12)
=>
昨天意味着 9-24 = -15h
虽然正确答案是 9
,为了解决这个问题,我们将使用 (a%n + n) % n
而 a%n == (a - n * Math.toIntExact(a/n))
然后 -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
}
您可以使用商和余数来计算计数和余数。
这是我为 Minecraft 插件编写的一些代码。
public class Division {
public static ModuloDivResult divideMod(double a, double b) {
double remainder = a % b;
return new ModuloDivResult((a / b) - (remainder / b), remainder);
}
}
我们还需要一个容器来容纳这两个值。
public class ModuloDivResult {
private double remainder;
private double count;
public ModuloDivResult(double count, double remainder) {
this.remainder = remainder;
this.count = count;
}
public double getRemainder() {
return remainder;
}
public double getCount() {
return count;
}
}