问题 C: 采用递归求第n位数(c#)

题目描述

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

输入

输入一个正整数,代表求第几位数字

输出

输出第n位数字

样例输入

30

样例输出

832040

提示

输入数字必须大于零

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Helloworld
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(calc(n));
            Console.ReadKey();
        }

        static int calc(int n)
        {
            if (n < 0) return 0;
            else if (n == 1)
            {
                return 1;
            }
            else
            {
                return calc(n - 1) + calc(n - 2);
            }
        }
    }

}

  

原文地址:https://www.cnblogs.com/mjn1/p/12402700.html