1、
The Fibonacci number sequence {F_N} is defined as: F_0=0, F_1=1, F_N=F_N−1+F_N−2, N=2, 3, .... The time complexity of the function which calculates F_N recursively is Θ(N!).
T()
F()
说实话我还没法一眼看出来……姑且写一下这个函数:
#include <stdio.h>
int Fib (int n);
int main () {
int n;
scanf ("%d", &n);
printf ("%d", Fib (n));
return 0;
}
int Fib (int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return Fib (n - 1) + Fib (n - 2);
}
分析该函数可知时间复杂度为 O(2^N),分析过程如下:
Fib(N) = Fib(N-1) + Fib(N-2) = [ Fib(N-2) + Fib(N-3) ] + [ Fib(N-3) + Fib(N-4) ] = ... ...
可见括号内参数每减1,整体项数就会乘2,故当出现Fib(0)时,共有2^N项
故答案为F
PS:该函数的空间复杂度为O(N),因为有N层
2、
Let n be a non-negative integer re