zoukankan      html  css  js  c++  java
  • 优化(1159斐波那契数)

    最近在网课学习中

    了解了一些基本算法

    其中在递归算法时

    在无优化递归斐波那契时

    程序效率低

    因为许多数在之前已经被算过一遍了

    所以使用记忆化搜索优化

    #include<iostream>
    #include<cstring>
    #include<Windows.h>
    using namespace std;
    int flog(int v);
    int s[1000];
    int main()
    {
        int n,k;
        cin>>n;
        DWORD startTime=GetTickCount();
        k=flog(n);
        DWORD endTime=GetTickCount();
        cout<<k<<" "<<endTime-startTime;
    }
    int flog(int v)
    {
        if(v==1)
          return 0;
        if(v==2)
          return 1;
        if(s[v]==0) s[v]=flog(v-1)+flog(v-2);
        return s[v];
    }
    

      

    对比不经过优化的

    #include<iostream>
    #include<Windows.h>
    using namespace std;
    int flog(int v);
    int main()
    {
        int n,k;
        cin>>n;
        DWORD startTime=GetTickCount();
        k=flog(n);
        DWORD endTime=GetTickCount();
        cout<<k<<" "<<endTime-startTime;
    }
    int flog(int v)
    {
        if(v==1)
          return 0;
        else if(v==2)
                return 1;
             else return flog(v-1)+flog(v-2);
        
       
          
        
    } 

     明显发现优化后效率更高.

  • 相关阅读:
    join函数——Gevent源码分析
    代理上网(ssh 动态端口转发)
    内核热patch
    技术债
    mysql 隔离级别与间隙锁等
    python type
    django : related_name and related_query_name
    ssh 卡主
    logistics regression
    __new__ 和 __init__
  • 原文地址:https://www.cnblogs.com/-Iris-/p/12245690.html
Copyright © 2011-2022 走看看