递归打印出指定index位置上的斐波那契数(c#实现)

题目:递归打印出指定index位置上的斐波那契数

using System;

namespace cchoop
{
    class Program
    {
        static void Main(string[] args)
        {
            //1 1 2 3 5
            Console.WriteLine(GetFibonacciNumber(5));
        }

        static int GetFibonacciNumber(int index)
        {
            if (index <= 0)
            {
                return 0;
            }
            if (index == 2 || index == 1)
            {
                return 1;
            }
            else
            {
                return GetFibonacciNumber(index - 2) + GetFibonacciNumber(index - 1);
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_34937637/article/details/81136447