Zzuli OJ 1113: Number of recursive calls statistics (function topic) __ function

Source: Internet
Author: User
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; 
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.