【发布时间】:2020-01-31 03:51:25
【问题描述】:
我正在尝试使用这个工作递归函数(下)...
def cubeIt(n: Int): Int = {
println("acc:"+acc);
println("n: "+n);
if(n==0){
return 0;
}
else if(n>0){
return cubeIt(n-1) + 3*(n*n) - 3*n + 1
}
else{
return cubeIt(n+1) - 3*(n*n) - 3*n - 1
}
}
...把它变成一个尾递归函数。
import scala.annotation.tailrec
@tailrec
def cubeItHelper(n: Int, acc: Int): Int = { /* Implement cubeIt as a tail recursion */
if(n==0){
return acc;
}
if(n>0){
return cubeItHelper(n-1, acc+(3*(n*n) - 3*n + 1));
}
else{
return cubeItHelper(n+1, (acc+((-3)*(n*n) - 3*n - 1)));
}
}
/*-
This is the main function that the users will use.
It should call the helper with the correct initial value of acc argument.
Just replace the ??? with the correct initial value for the acc argument.
-*/
def cubeItTail(n: Int): Int = {
if(n==0){
return 0;
}
else{
cubeItHelper(n, 0);
}
}
使用上面的代码,它会输出:
acc:0
n: 5
acc:61
n: 4
acc:98
n: 3
acc:117
n: 2
acc:124
n: 1
......
前四个测试行正确完成
n: -100
acc:-29701
n: -99
acc:-58808
n: -98
acc:-87327
...(goes forever)
这些是测试语句(它挂在最后一个):
assert(cubeItTail(5) == 125)
assert(cubeItTail(-5) == -125)
assert(cubeItTail(-3) == -27)
assert(cubeItTail(0) == 0)
assert( (-100 to 100).forall( x => (cubeItTail(x) == x * x * x)), "Test Passed!")
如果有人能告诉我我在这里做错了什么,我将不胜感激。
【问题讨论】:
-
您删除的指纹是问题的根本原因吗?如果是这样,您应该将它们保留在问题中/
-
好吧我更新它
-
这里提问有两个方面,一是得到答案,二是把这个答案留给以后有同样问题的人找到,不用再问
标签: scala recursion tail-recursion tail