实际代码更容易,但我也很难找到基本情况。我能够编写相当不错的伪代码,但我遇到了麻烦。我不知道我是否可以在这里问作业问题,但这是一个我无法回答的问题:
Let f(n) be the number of additions performed by this computation. Write a recurrence equation for f(n). (Note that the number of addition steps should be exactly the same for both the non-recursive and recursive versions. In fact, they both should make exactly the same sequence of addition steps.)
如果我不被允许问作业问题,任何帮助都会很棒。
int sum(int A[], int n ):
T=A[0];
for i = 1; to n-1
T=T+A[i];
return T;}
最佳答案
使用 sum 函数的以下属性:
sum(A[], n) == sum(A[], n-1) + A[n]
并考虑到:
sum(A[], 1) == A[1]
关于java - 我如何编写伪代码的递归函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15958777/