fork download
  1. #include <algorithm>
  2. #include <iomanip>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int totalDepth;
  8.  
  9. int fib_rec(int n, int depth=0)
  10. {
  11. totalDepth = max(totalDepth,depth);
  12. switch(n){
  13. case 0: return 0;
  14. case 1: return 1;
  15. default: return fib_rec(n-1,depth+1)+fib_rec(n-2,depth+1);
  16. }
  17. }
  18.  
  19. int main()
  20. {
  21. #define DBG(x) { auto ret = (x); cout << left << setw(10) << (ret); cout << "depth:" << totalDepth << endl; }
  22.  
  23. DBG(fib_rec(1));
  24. DBG(fib_rec(3));
  25. DBG(fib_rec(5));
  26. DBG(fib_rec(7));
  27. }
  28.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
1         depth:0
2         depth:2
5         depth:4
13        depth:6