我的任务是在MIPS中实现递归函数。这个功能是
function1(n)= n-5(如果n <= 3)
否则= 4 * function1(n-1) - n * function1(n-3)
其中一个测试用例是n = 6,结果是200 如何在此功能中输入6来获得200?对我来说,看起来答案应该是2.是否有一些我不了解递归,或者我是否理解函数错误?我很困惑
答案 0 :(得分:2)
你似乎在某处误解了这个功能。以下是我到达200时所采取的步骤:
function1(6) = 4*function1(5) - 6*function1(3) (by rule 2)
function1(5) = 4*function1(4) - 5*function1(2) (by rule 2)
function1(4) = 4*function1(3) - 4*function1(1) (by rule 2)
function1(3) = 3-5 = -2 (by rule 1)
function1(2) = 2-5 = -3 (by rule 1)
function1(1) = 1-5 = -4 (by rule 1)
代替......
function1(4) = 4*-2 - 4*-4 = -8 - -16 = 8
function1(5) = 4*8 - 5*-3 = 32 - -15 = 47
function1(6) = 4*47 - 6*-2 = 188 - -12 = 200