我正在尝试获取数字的阶乘值66,但是我的方法给了我一个输出0。但每当我尝试获取阶乘5,这给我带来了输出120。谁能告诉我为什么吗?
public static int factorial(int n)
{
if (n == 1)
return n;
return n * factorial(n - 1);
}
当然 - 阶乘变得非常大、非常快。你溢出了 int 的界限very很快...并且在某个时刻,您将乘以足够的因子以使溢出为 0,然后该值将永远保持为 0。
According to a quick Google search, 66 factorial is 5.44344939 × 1092 - which is considerably more than int
can handle, or even long
or decimal
. You could get double
to handle it - you'd lose a huge amount of precision, and that would accumulate really quickly too, but at least it wouldn't overflow...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)