Advertisement
Guest User

PROBLEM 1029 - FIBONACCI, HOW MANY CALLS?

a guest
Apr 30th, 2014
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int counter, call;
  5.  
  6. int fibonacci(int n)
  7. {
  8.     if(n == 0){
  9.         call++;
  10.         return 0;
  11.     }else if(n == 1){
  12.         call++;
  13.         counter++;
  14.         return 1;
  15.     }else{
  16.         call++;
  17.         return fibonacci(n - 1) + fibonacci(n - 2);
  18.     }
  19. }
  20.  
  21. int main()
  22. {
  23.     int n, x, res;
  24.  
  25.     cin >> n;
  26.  
  27.     for (int i = 0; i < n; ++i)
  28.     {
  29.         counter = 0;
  30.         call = 0;
  31.         cin >> x;
  32.  
  33.         res = fibonacci(x);
  34.  
  35.         cout << "fib(" << x << ") = " << call << " calls = " << counter << endl;
  36.  
  37.     }
  38.  
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement