C#求斐波那契数列第30项的值(递归和非递归)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 斐波那契数列求和
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("第30项的值是:{0}", Fn(30));
            Console.WriteLine("第30项的值是:{0}", Fnl(30));
            Console.WriteLine("第30项的值是:{0}", Fn2(30));
            Console.ReadKey();
        }
        //递归算法
        static int Fn(int n)
        {
            if (n <= 0)
                return 0;
            if (n == 1 || n == 2)
                return 1;
            return Fn(n - 1) + Fn(n - 2);
        }
        //一般实现
        static int Fnl(int n)
        {
            if (n <= 0)
                return 0;
            int a = 1;
            int b = 1;
            int c = 1;
            for (int i = 3; i <= n; i++)
            {
                c = checked(a + b);
                a = b;
                b = c;
            }
            return c;
        }
        //两个临时变量进行计算
        static int Fn2(int n)
        {
            if (n <= 0)
                return 0;
            int a = 1;
            int b = 1;
            for (int i = 3; i <= n; i++)
            {
                b = checked(a + b);
                a = b - a;
            }
            return b;
        }
    }
}
View Code

算法方面的时间复杂度问题,自己了解一点,知道怎么算,但还是觉得掌握的不够好,不能充分去评估一个算法的效率。

原文地址:https://www.cnblogs.com/zuimeideshi520/p/6025416.html