Java 中的模数 [英] Modulus in Java
问题描述
我想在 Java 中获取包含模数 %
的方程中未知数的值
I want to get the value of an unknown number in equation containing modulus %
in Java
例如:
x % 26 = y
如果我有 y
的值,我怎样才能得到 x
x % 26 = y
if I have the value of y
how can I get x
推荐答案
问题是存在零解(如果 Math.abs(y) >= 26
)或无限1 满足给定 y
等式的 x
值的数量.一般的答案是:
The problem is that there are either zero solutions (if Math.abs(y) >= 26
) or an infinite1 number of values of x
that satisfy that equation for a given y
. The general answer is:
x = 26 * k + y
对于 k
的任何整数值.你可以选择任何你想要的 k
.2
for any integer value of k
. You can pick whatever k
you want.2
1 实际上,范围将受您使用的整数值范围的限制.如果x
和y
是int
值,那么您将受到Integer.MAX_VALUE
和Integer 的限制.MIN_VALUE
.另一方面,如果它们是 BigInteger
值,则您没有太多范围限制.
1 In practice, the range will be limited by the range of integer values you are using. If x
and y
are int
values, then you are limited by Integer.MAX_VALUE
and Integer.MIN_VALUE
. On the other hand, if they are BigInteger
values, you don't have much in the way of range constraints.
2 其实Java中x
和y
的符号一定是一样的,所以你只能选择无穷大的一半.:-)
2 Actually, the signs of x
and y
must be the same in Java, so you only have half of infinity to pick from. :-)
这篇关于Java 中的模数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!