递归

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


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

 

namespace ConsoleApplication1
{
    class PhabeRecur_递归
    {
        /// <summary>
        /// 返回斐波那契数列中得索引为 index d的 数值
        /// </summary>
        /// <param name="index">数列中元素的索引(下标)</param>
        /// <returns>数列中索引为index 的数值</returns>
        internal static long GetNumber(int index)
        {
            if (index == 0 || index == 1)
            {
                return 1;
            }
            //return GetNumber(index - 1) + GetNumber(index);
            long first = GetNumber(index - 1);
            long second = GetNumber(index - 2);
            return first + second;
        }
    }
    class Program
    {
        //Main 函数是整个命令控制台应用程序的入口点Entry Point(起点)
        static void Main(string[] args)
        {
            Console.WriteLine(PhabeRecur_递归.GetNumber(5));
         
            Console.Read();
        }
    }
}

posted @ 2012-05-07 17:59  |▍花舞花落泪 ╮  阅读(149)  评论(0编辑  收藏  举报