编写一个程序来计算两个数相除的余数,而不使用 % 运算符?在 Java 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21369414/
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
Write a program to find remainder of dividing two numbers, without using % operator? In Java
提问by Django
How to find the remainder of dividing two numbers without using the modulo operator!!
My teacher gave me this exact exercise and It's only my 5th lecture in a course called programming fundamentals.
I have already tried this equation,
如何在不使用模运算符的情况下找到两个数字相除的余数!!我的老师给了我这个确切的练习,这只是我在编程基础课程中的第 5 堂课。
我已经尝试过这个等式,
a%b = a - (a/b)*b
but it always returns zero!
但它总是返回零!
采纳答案by BlueMoon93
I've just tried this
我刚试过这个
public static void main (String [] args){
int a = 50;
int b = 9;
int c = a%b;
int d = a - (a/b)*b;
System.out.println(c);
System.out.println(d);
}
And it seems to work. What types are your variables?
它似乎有效。你的变量是什么类型?
回答by Reinstate Monica
Well, you could do what the modulo operator typically does internally.
好吧,您可以执行模运算符通常在内部执行的操作。
Inside a loop, substract b
from a
repeatedly. When this is no longer possible, the number you're left with is your answer.
在循环内,b
从a
重复减去。当这不再可能时,您留下的数字就是您的答案。
Here's a code example. If you're trying to accomplish this for decimal numbers, be mindful of rounding errors for floating-point values.
这是一个代码示例。如果您尝试对十进制数完成此操作,请注意浮点值的舍入错误。
double result = a;
while(result - b >= 0) {
result -= b;
}
return result;
Note that if you're working with integers, and you're allowed to use the division operator, you don't need to use a loop.
请注意,如果您正在处理整数,并且可以使用除法运算符,则不需要使用循环。
However, do keep in mind that all that division does is repeated subtraction. If this is for pedagogical purposes, I think it's cooler to do it my way.
但是,请记住,除法所做的只是重复减法。如果这是出于教学目的,我认为按照我的方式做会更酷。
回答by Christian
When using double
s, that equation will always return 0
. When using int
that equation must work because of truncation (integer division).
使用double
s 时,该等式将始终返回0
。int
由于截断(整数除法),使用该方程时必须有效。
Here is the algorithm to compute the remainder in a division. Let's say you want to find a % b
:
这是计算除法余数的算法。假设您要查找a % b
:
while dividend is larger or equal than divisor
substract divisor from dividend
print last result
Implemented in Java it will look like:
在 Java 中实现它看起来像:
public static void main(String[] args)
{
double a = 5, b = 2;
while (a >= b) {
a -= b;
}
System.out.println(a);
}
回答by Dawood ibn Kareem
It makes no sense to perform %
on double
or float
numbers, because floating point numbers actually each represent a very small range of values, rather than an exact value. Often, the internal representation of a floating point number isn't the same number in that range as the actual decimal that you used to create the number. This means that the result of %
is formally undefined, for floating point types.
%
对double
或float
数字执行是没有意义的,因为浮点数实际上每个都代表一个非常小的值范围,而不是一个精确的值。通常,浮点数的内部表示与用于创建该数字的实际小数点在该范围内的数字不同。这意味着%
对于浮点类型,结果是形式上未定义的。
If you try 0.3 % 0.1
in Java, you actually get 0.09999999999999998
, which seems at first sight to be incorrect; but this makes as much sense as any other result, given what floating point numbers really represent.
如果您尝试0.3 % 0.1
使用 Java,您实际上会得到0.09999999999999998
,这乍一看似乎是不正确的;但考虑到浮点数真正代表什么,这与任何其他结果一样有意义。
Here is the simplest solution for working out %
, without actually using %
.
这是最简单的锻炼解决方案%
,无需实际使用%
.
a - b * (long)(a/b);
It will perform better than an answer that does repeated subtraction, because it doesn't have to loop over and over.
它将比重复减法的答案表现更好,因为它不必一遍又一遍地循环。
But this solution inevitably falls victim to the same trap as certain other answers on this page, namely that the "correct" answer is formally undefined.
但是这个解决方案不可避免地成为与此页面上某些其他答案相同的陷阱的受害者,即“正确”答案在形式上是未定义的。
This solution will also give overflow problems if your numbers are extremely large.
如果您的数字非常大,此解决方案也会出现溢出问题。
回答by Jamie T
You need to take the Math.floor of a/b.
您需要使用 a/b 的 Math.floor。
you have a%b = a - (a/b)*b but really a%b= a- (Math.floor(a/b)*b)
你有 a%b = a - (a/b)*b 但实际上 a%b= a- (Math.floor(a/b)*b)
This is because without the math.floor it includes the decimal remainder and (a/b)*b is equal to a so when you subtract it from a you will get 0 every time, just as you experienced.
这是因为没有 math.floor 它包括十进制余数并且 (a/b)*b 等于 a 所以当你从 a 中减去它时,你每次都会得到 0,就像你所经历的那样。