zoukankan      html  css  js  c++  java
  • 数据结构算法之斐波那契数列

    斐波那契数列:1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233……

    第一项和第二项是1,之后的每一项为之前两项的和。

    递推:从初值出发反复进行某一运算得到所需结果。-----从已知到未知,从小到达(比如每年长高9cm,20年180,30后270)

    递归:从所需结果出发不断回溯前一运算直到回到初值再递推得到所需结果----从未知到已知,从大到小,再从小到大(你想进bat,那么编程就的牛逼,就得卸载玩者农药,努力学习)。递归(Recursion)是从归纳法(Induction)衍生出来的

     
    递归算法:
        class Program
        {
            static void Main(string[] args)
            { 
                GetNumberAtPos(12);
            }
    
            static long GetNumberAtPos(int n)
            { 
                return GetNumberAtPos(1, 1, 3, n);
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="fist"></param>
            /// <param name="second"></param>
            /// <param name="i">第几次了</param>
            /// <param name="n">第n项</param>
            /// <returns></returns>
            static long GetNumberAtPos(long fist, long second, int i, int n)
            {
                if(second==1)
                {
                    Console.WriteLine(1);
                    Console.WriteLine(1);
                }
                //currentCount是第n项的值
                long currentCount = fist + second;
                Console.WriteLine(currentCount.ToString());
                if (i < n)
                {
                    long next = GetNumberAtPos(second, currentCount, ++i, n);
    
                }
                //else
                //{
                //    Console.WriteLine(currentCount.ToString());
                //}
    
                return currentCount;
    
            }
    
    
    
        }

    结果:

    1
    1
    2
    3
    5
    8
    13
    21
    34
    55
    89
    144

    如果是想打印第n项的数据:

        class Program
        {
            static void Main(string[] args)
            {
                //从第三项开始,运行到第100项
                 GetNumberAtPos(1, 1, 3, 100);
            }
    
            static long GetNumberAtPos(long first, long second, int i, int n)
            {
                long third = first + second;
                if (i < n)
                {
                    long next = GetNumberAtPos(second, third, ++i, n);
                }
                else
                {
                    Console.WriteLine(third.ToString());
                }
    
                return third;
            }
    
    
    
        }

    结果:3736710778780434371

    递推法求大于100是第几项:

       static void Main(string[] args)
            {
    
                long fn_1 = 1, fn_2 = 1, fn = 0;
                int i = 3;
                while (true)
                {
                    fn = fn_1 + fn_2;
                    if (fn > 100)
                    {
                        Console.WriteLine(i);
                        break;
                    }
    
                    fn_1 = fn_2;
                    fn_2 = fn; 
                    i++;
                }
    
                //从第三项开始,运行到第100项
                //GetNumberAtPos(1, 1, 3, 100);
            }

    结果:12

     
  • 相关阅读:
    jquery通过live绑定toggle事件
    svn is already locked解决方案
    不安全代码只会在使用 /unsafe 编译的情况下出现
    eclipse 新建 java 文件时自动生成注释
    浅谈权限设计
    ie6/IE8/IE9/谷歌以及火狐等浏览器下li 高度一致解决办法
    PHP+MSSQL TEXT字段被截断的解决方案
    利用CSS让dl dt dd呈现多行多列效果
    CSS实现图片水平垂直居中于DIV
    CSS 关于IE6 margin 为负数 负值的时候 正常显示的方法
  • 原文地址:https://www.cnblogs.com/anjingdian/p/15391002.html
Copyright © 2011-2022 走看看