• Fibonacci数列计算的三种方法


    以下分别用递归,迭代,动态规划求解Fibonacci数列:

     1 #include <iostream>
    2 #include <ctime>
    3 using namespace std;
    4
    5 int Fibonacci_R(int n)
    6 {
    7 if((n == 1)||(n == 2))
    8 return 1;
    9 else
    10 return Fibonacci_R(n-1) + Fibonacci_R(n-2);
    11 }
    12
    13 int Fibonacci_I(int n)
    14 {
    15 int A = 1;
    16 int B = 1;
    17 int C = 2;
    18 int index;
    19 if((n == 1)||(n == 2))
    20 return 1;
    21 for(index = 3; index <= n; ++index)
    22 {
    23 C = A + B;
    24 A = B;
    25 B = C;
    26 }
    27 return C;
    28 }
    29
    30 int Fibonacci_D(int n)
    31 {
    32 int * pCache = new int[n]; //avoid stack overflow
    33 pCache[0] = 1;
    34 pCache[1] = 1;
    35 for(int i = 2; i<n; ++i)
    36 {
    37 pCache[i] = pCache[i-1] + pCache[i-2];
    38 }
    39 int result = pCache[n-1];
    40 delete [] pCache;
    41 return result;
    42 }
    43
    44 int main()
    45 {
    46 int n = 30;
    47 clock_t begin, end;
    48
    49 begin = clock();
    50 cout<<"Fibonacci_R "<<n<<" = "<<Fibonacci_R(n);
    51 end = clock();
    52 cout<<" time cost is "<<(end-begin)<<" ms"<<endl;
    53
    54 begin = clock();
    55 cout<<"Fibonacci_I "<<n<<" = "<<Fibonacci_I(n);
    56 end = clock();
    57 cout<<" time cost is "<<(end-begin)<<" ms"<<endl;
    58
    59 begin = clock();
    60 cout<<"Fibonacci_D "<<n<<" = "<<Fibonacci_D(n);
    61 end = clock();
    62 cout<<" time cost is "<<(end-begin)<<" ms"<<endl;
    63
    64 return 0;
    65 }

      后来想想,使用数组的方法跟迭代没什么区别,算不上动态规划,修改如下:

     1 #include <iostream>
    2 #include <map>
    3 #include <ctime>
    4
    5 using namespace std;
    6
    7 int Fibonacci_R(int n)
    8 {
    9 if((n == 1)||(n == 2))
    10 return 1;
    11 else
    12 return Fibonacci_R(n-1) + Fibonacci_R(n-2);
    13 }
    14
    15 int Fibonacci_I(int n)
    16 {
    17 int A = 1;
    18 int B = 1;
    19 int C = 2;
    20 int index;
    21 if((n == 1)||(n == 2))
    22 return 1;
    23 for(index = 3; index <= n; ++index)
    24 {
    25 C = A + B;
    26 A = B;
    27 B = C;
    28 }
    29 return C;
    30 }
    31
    32 //int Fibonacci_D(int n)
    33 //{
    34 // int * pCache = new int[n];
    35 // pCache[0] = 1;
    36 // pCache[1] = 1;
    37 // for(int i = 2; i<n; ++i)
    38 // {
    39 // pCache[i] = pCache[i-1] + pCache[i-2];
    40 // }
    41 // int result = pCache[n-1];
    42 // delete [] pCache;
    43 // return result;
    44 //}
    45
    46
    47 int Fibonacci_D(int n, map<int, int> & Cache)
    48 {
    49 map<int, int>::const_iterator iLocator = Cache.find(n);
    50 if(iLocator != Cache.end())
    51 return iLocator->second;
    52 else
    53 Cache.insert(make_pair(n, Fibonacci_D(n - 1, Cache) + Fibonacci_D(n - 2, Cache) ));
    54 iLocator = Cache.find(n);
    55 return iLocator->second;
    56 }
    57
    58
    59 int main()
    60 {
    61 int n = 30;
    62 clock_t begin, end;
    63 map<int, int> Fibonacci_Cache;
    64 Fibonacci_Cache.insert(make_pair(1, 1));
    65 Fibonacci_Cache.insert(make_pair(2, 1));
    66
    67 begin = clock();
    68 cout<<"Fibonacci_R "<<n<<" = "<<Fibonacci_R(n);
    69 end = clock();
    70 cout<<" time cost is "<<(end-begin)<<" ms"<<endl;
    71
    72 begin = clock();
    73 cout<<"Fibonacci_I "<<n<<" = "<<Fibonacci_I(n);
    74 end = clock();
    75 cout<<" time cost is "<<(end-begin)<<" ms"<<endl;
    76
    77 begin = clock();
    78 cout<<"Fibonacci_D "<<n<<" = "<<Fibonacci_D(n, Fibonacci_Cache);
    79 end = clock();
    80 cout<<" time cost is "<<(end-begin)<<" ms"<<endl;
    81
    82 return 0;
    83 }

      当计算数字很大时,迭代速度 > 动态规划(map操作耗费的时间?) >>> 递归。

      

      

  • 相关阅读:
    freeswitch与外部网关链接
    dojo中DateTextBox日期格式yyyy-MM-dd转化为数据库中yyyyMMdd
    dojo中取DateTextBox中的值两种方法
    视频编码与封装方式详解
    音频编码汇总
    FusionCharts 3D双柱状图
    FusionCharts 2D双柱状图
    利用merge优化
    FusionCharts 2D条状图
    全表扫描出现db file sequential read
  • 原文地址:https://www.cnblogs.com/DanielZheng/p/2149948.html
Copyright © 2020-2023  润新知