Description
The function of the following program is to compute the nth term of the Fibonacci sequence. The function fib () is a recursive function. Please rewrite the program, calculate the nth item, and count the number of times the function fib (including main () call to FIB () is called).
#include <stdio.h>
int fib (int k);
int main (void)
{
int n;
scanf ("%d", &n);
printf ("%d\n", FIB (n));
return 0;
}
int fib (int k)
{
if (k = = 1 | | k = = 2)
return 1;
Else
return fib (k-1) + fib (k-2);
} Input
Enter a positive integer n. Output
The input contains two rows, the first row is an integer, the value of the nth item, and the second line of input recursive calls, the specific format see output sample.
Sample Input sample Output 6765 recursive call 13,529 times HINT
Source
#include <stdio.h>
int i=0;
int fib (int k);
int b (int n);
int main (void)
{
int n,m;
scanf ("%d", &n);
b (n);
}
int b (int n)
{
printf ("%d\n", FIB (n));
printf ("Recursive call%d times", i);
}
int fib (int k) {
i++;
if (k = = 1 | | k = = 2) {return
1;
}
else{return
fib (k-1) + fib (k-2);
}
return i;
}