日期:2014-05-17 浏览次数:21175 次
* 作 者: 雷恒鑫 
* 完成日期: 2012 年 09 月 09 日
* 版 本 号: V1.0 
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication_do_while
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("这是一个“一列数规则如下:1,1,2,3,5,8,13,21,34...求第30位数是多少?”的程序");
            Console.Write("请问您想求第几位数?");
            int number = int.Parse(Console.ReadLine());
            int m = f(number);//递归函数
            Console.WriteLine("第{0}位数为:{1}", number, m);
            Console.ReadKey();
        }
        static int f(int number)//递归就是在过程或函数里调用自身。 
        {
            int fact;
            if (number == 0 || number == 1)//使用递归策略时,必须有一个明确的递归结束条件,称为递归出口。
            {
                if (number == 0)//当number等于2是有0这种情况出现
                {
                    fact = 0;
                }
                else
                {
                    fact = 1;
                }
            }
            else
            {
                fact = f(number - 1) + f(number - 2);
            }
            return fact;
        }
    }
}
           
    
运行结果:


