又是一个递归的题

一列数的规则如下: 1、1、2、3、5、8、13、21、34......  求第30位数是多少, 用递归算法实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Foo(20));
            Console.ReadLine();
        }
        public static int Foo(int i)
        {
            if (i <= 0)
            {
                return 0;
            }
            else if (i >= 1 && i <= 2)
            {
                return 1;
            }
            else
            {
                return Foo(i - 1) + Foo(i - 2);
            }
        }
    }
}

 

原文地址:https://www.cnblogs.com/fumj/p/2619009.html