我将如何着手使以下阿克曼函数“学习”并记住先前迭代的结果,以便它不会一遍又一遍地重新计算相同的东西?例如:它应该知道,只要给定输入 (2,2),它就应该返回 7 而无需再次运行计算。
public static BigInteger ackermann(BigInteger a, BigInteger b) {
if (a.equals(BigInteger.ZERO)) {
return b.add(BigInteger.ONE);
}
if (b.equals(BigInteger.ZERO)) {
return ackermann(a.subtract(BigInteger.ONE),BigInteger.ONE);
}
return ackermann(a.subtract(BigInteger.ONE), ackermann(a, b.subtract(BigInteger.ONE)));
}
最佳答案
看看使用 Java 8 的内存:https://dzone.com/articles/java-8-automatic-memoization .这是迄今为止实现您的目标的最优雅的方式。
关于java - 你如何制作阿克曼函数 "learn"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33056734/