DP-- memory search

1  // Fibonacci recursion simple display of Number (large memory footprint) (strictly speaking not an algorithm) 
 2  // using the formula = AN-1 + AN-2 AN (n-> 1) 
. 3  / * #include <the iostream>
 . 4  the using namespace STD;
 . 5  int FIB (int);
 . 6  int main ()
 . 7  {   
 . 8      for (int I = 0; I <40; I ++)
 . 9         COUT << FIB (I) << endl;    
 10      return 0;    
 . 11  } 
 12 is  int FIB (n-int)
 13 is  {
 14      return (n-<=. 1) n-:? FIB (. 1-n-) + FIB (n--2);
 15  } * / 
16  // memory search improvement 
. 17  
18 is #include <the iostream>
19 #include<cstring>
20 using namespace std;
21 const int MAXN=100;
22 int memo[MAXN];
23 int fib(int);
24 int k=0;
25 int main()
26 {   
27     for(k=0;k<MAXN;k++)
28       memo[k]=0;
29     for(int i=0;i<40;i++)
30        cout<<fib(i)<<endl;    
31     return 0;    
32 } 
33 int fib(int n)
34 {
35     if(n<=1) return n;
36     if(memo[n]!=0) return memo[n];
37     return memo[n]=fib(n-1)+fib(n-2);
38 }

 

1 // 求第n项fib数
2     int f[150];
3     f[1] = f[2] = 1;
4     for(int i = 3;i < 110;i++)
5         f[i] = f[i-1] + f[i-2];

 

Guess you like

Origin www.cnblogs.com/TYXmax/p/10994303.html