一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少?

不使用递归

static int fun(int x1, int x2, int frequency)
        {
            int temp = 0;
            if (frequency <= 0)
                return 0;
            else if (frequency == 1)
                return x1;
            else if (frequency == 2)
                return x2;
            else
            {
                for (int i = frequency - 2; i >= 1; i--)
                {
                    temp = x1 + x2;
                    x1 = x2;
                    x2 = temp;
                }
                return temp;
            }
        }

递归

        static int fun(int i)
        {
            if (i <= 0)
            {
                return 0;
            }
            else if (i > 0 && i <= 2)
            {
                return 1;
            }
            else
            {
                return fun(i - 2) + fun(i - 1);
            }
        }

 

posted @ 2013-06-20 16:02  武沛齐  阅读(492)  评论(0编辑  收藏  举报