现在的位置: 首页 > 综合 > 正文

简单的递归

2014年03月24日 ⁄ 综合 ⁄ 共 470字 ⁄ 字号 评论关闭

age(int n)
{
         int c;
         if (n==1)
               c=10;
         else
               c=age(n-1)+2;
        return(c);
}

main()
{
printf("%d",age(5));
}

是C程序中递归调用的最简实例.初学递归 不明白此程序的运行方式.为何最后...

private static int f(int n)
{
       int c;
       if(n==1||n==2)
       {
              c = 1;
       }
       else
       {
               c = f(n-1) + f(n-2);
       }
       return c;
}

static void Main(string[] args)
{
       for(int i=1;i<=30;i++)
       {
              Console.WriteLine(f(i));
       }
       Console.ReadLine();
}

 

抱歉!评论已关闭.