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

昨天面试的一条要求用递归方法实现的序列

2012年11月06日 ⁄ 综合 ⁄ 共 275字 ⁄ 字号 评论关闭

要求用控制台应用程序写出如下1,1,2,3,5,8,13,21,34,55,89...

当时没想开,今天调试一下出来了结果

如下

class Program
    {
        static void Main(string[] args)
        {
            Num(0, 1,1, 30);
            Console.ReadLine();
        }

        private static int Num(int num, int s, int level, int count)
        {
            Console.Write("{0},", s);
            if (level < count)
            {
                num = Num(s, (s + num), ++level, count);
            }
            return num;
        }
    }

抱歉!评论已关闭.