我正在尝试比较那些大到连BigIntegers都无法处理的数字。我的解决方案是将数字转换为字符串,并对它们进行字符串比较。
这样行得通吗?我真的不确定如何实现这样的东西。我正在尝试对一个算法进行单元测试,以便为我被吸进去的项目Euler程序生成1000的阶乘。
发布于 2011-09-27 08:44:19
你的假设是错误的。
BigInteger提供任意精度,因此它绝对可以处理这么大的数字。
尝试以下操作:
public class Main {
public static void main(String[] args) {
BigInteger thousand = BigInteger.valueOf(1000L);
for (int i = 999; i > 0; i--)
{
thousand = thousand.multiply(BigInteger.valueOf(i));
}
System.out.println(thousand.toString());
}
}https://stackoverflow.com/questions/7562958
复制相似问题