2019.2.22
开始学习CS61B
一、斐波那契数列
两个版本
递归(tree recursive)

public static int fib1(int n)
{
    if(n<=1){
        return n;
    }
    else
        return fib1(n-1)+fib1(n-2);
}

尾递归(tail recursive)

public static int fib2(int n, int k, int f0, int f1)
{
    if(k==n)
    {
        return f1;
    }
    else
        return fib2(n,k+1,f1,f1+f0);
}

CS61B Discussion1

总结:尾递归不需要计算已经知道的值.

http://www-inst.eecs.berkeley.edu/~cs61b/fa14/ta-materials/discussion1sol.pdf

相关文章:

  • 2018-07-22
  • 2021-10-01
  • 2021-04-09
  • 2021-07-28
  • 2021-08-07
  • 2021-08-02
  • 2021-11-03
  • 2021-11-24
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2021-08-28
  • 2021-06-14
  • 2021-09-12
相关资源
相似解决方案