Advertisement
apl-mhd

fiboUsingRecursionAndDP

Oct 20th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.60 KB | None | 0 0
  1. #include <QCoreApplication>
  2. #include <cstdio>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7.  
  8.  
  9.  //int number[100];
  10.  vector <int> dp (100,-1);
  11.  
  12.  
  13.  
  14.  
  15. int fibo(int n){
  16.  
  17.     dp[0]= dp[1]=1;
  18.  
  19.     if( n == 0 || n == 1 )
  20.  
  21.             return 1;
  22.  
  23.         if(dp[n] != -1)
  24.             printf(" %d ", dp[n]);
  25.             return dp[n];
  26.  
  27.  
  28.     return dp[n] =  fibo(n-1) + fibo(n-2);
  29.  
  30. }
  31.  
  32.  
  33.  
  34. int main(int argc, char *argv[])
  35. {
  36.  
  37.  
  38.  //printf(" %d ", numbers2[4]);
  39.  
  40.  
  41.  
  42.     int n;
  43.  
  44.         fibo(5);
  45.         for(n = 0; n< 5; n++){
  46.  
  47.             printf(" %d \n", dp[n]);
  48.         }
  49.  
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement