Fork me on GitHub

题目描述

大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。 n<=39

解题思路

递推公式f(n)=f(n)=
当n=0=0,当n=0 当
n=1=1,当n=1
其他=f(n−1)+f(n−2)看到这大家很容易想起递归,课堂上老师讲递归的时候的经典例子。但是当n很大的时候,就会出现堆栈溢出。堆栈溢出的主要原因是,递归重复的计算太多,很多计算是可以避免的,用循环计算结果,显根据前两项算出第三项,以后每次都是这样计算。

代码实现

递归实现

        public static int Fibonacci(int n) {
            if (n <= 1) return n;

            return Fibonacci(n - 1) + Fibonacci(n - 2);
        }

循环实现

        public static int Fibonacci2(int n)
        {
            if (n <= 1) return n;

            int first = 0;
            int second = 1;
            int result = 0;
            for (int i = 2; i <= n; i++)
            {
                result = first + second;
                first = second;
                second = result;
            }

            return result;
        }

斐波那契数列求和

        public static int FibonacciSum(int n) {
            if (n <= 1) return n;
            int first = 0;
            int second = 1;
            int temp = 0;
            int result = first + second;
            for (int i = 2; i <= n; i++) {
                temp = first + second;
                first = second;
                second = temp;

                result = result + temp;
            }

            return result;
        }

斐波那契数列求和,利用公式计算

        public static int FibonacciSum2(int n)
        {
            if (n <= 1) return n;
            int first = 0;
            int second = 1;
            int temp = 0;
            for (int i = 2; i <= n; i++)
            {
                temp = first + second;
                first = second;
                second = temp;
            }

            int result = 2 * second + first - 1; //Sn = 2an + an - 1 - 1

            return result;
        }

测试

        [Fact]
        public void Test0()
        {
            Assert.Equal(0, Coding007.Fibonacci(0));
            Assert.Equal(0, Coding007.Fibonacci2(0));
            Assert.Equal(0, Coding007.FibonacciSum(0));
            Assert.Equal(0, Coding007.FibonacciSum2(0));
        }

        [Fact]
        public void Test1()
        {
            Assert.Equal(1, Coding007.Fibonacci(1));
            Assert.Equal(1, Coding007.Fibonacci2(1));
            Assert.Equal(1, Coding007.FibonacciSum(1));
            Assert.Equal(1, Coding007.FibonacciSum2(1));
        }

        [Fact]
        public void Test2()
        {
            Assert.Equal(1, Coding007.Fibonacci(2));
            Assert.Equal(1, Coding007.Fibonacci2(2));
            Assert.Equal(2, Coding007.FibonacciSum(2));
            Assert.Equal(2, Coding007.FibonacciSum2(2));
        }

        [Fact]
        public void Test3()
        {
            Assert.Equal(2, Coding007.Fibonacci(3));
            Assert.Equal(2, Coding007.Fibonacci2(3));
            Assert.Equal(4, Coding007.FibonacciSum(3));
            Assert.Equal(4, Coding007.FibonacciSum2(3));
        }

        [Fact]
        public void Test4()
        {
            Assert.Equal(3, Coding007.Fibonacci(4));
            Assert.Equal(3, Coding007.Fibonacci2(4));
            Assert.Equal(7, Coding007.FibonacciSum(4));
            Assert.Equal(7, Coding007.FibonacciSum2(4));
        }

        [Fact]
        public void Test5()
        {
            Assert.Equal(5, Coding007.Fibonacci(5));
            Assert.Equal(5, Coding007.Fibonacci2(5));
            Assert.Equal(12, Coding007.FibonacciSum(5));
            Assert.Equal(12, Coding007.FibonacciSum2(5));
        }

        [Fact]
        public void Test6()
        {
            Assert.Equal(8, Coding007.Fibonacci(6));
            Assert.Equal(8, Coding007.Fibonacci2(6));
            Assert.Equal(20, Coding007.FibonacciSum(6));
            Assert.Equal(20, Coding007.FibonacciSum2(6));
        }
View Code

想入非非:扩展思维,发挥想象

1. 熟悉递归
2. 熟悉斐波那契数列
3. 斐波那契数列求和
4. 知道有公式的就用公式,不要自己去循环就算,就像1+2+3+......,用高斯定理直接算结果,不要再循环了

posted on 2019-07-09 16:19  lingfeng95  阅读(1551)  评论(0编辑  收藏  举报