递归算法

public int ShuZi(int i)
{
  int sum = 0;
  if (i <= 0)
  {
    sum = 0;
  }
  else if (i == 1 || i == 2)
  {
    sum = 1;
  }
  else
  {
    sum = ShuZi(i-1) + ShuZi(i-2);
  }
  return sum ;
}

 

static void Main(string[] args)
{
  Program pg = new Program();
  Console.Write("请输入数字:");
  int shuzi= int.Parse(Console.ReadLine());
  int zongshu = pg.ShuZi( shuzi);
  Console.Write("第"+shuzi+"个数的数字是:"+zongshu);
  Console.ReadLine();
}

posted @ 2016-02-19 09:55  最讨厌起名字什么的了  阅读(292)  评论(0编辑  收藏  举报