斐波那数列(从第3项开始,每项都是前2项的和)

核心代码

        [HttpPost]
        public IActionResult FeiBoNa(int f) {
            string mes = "";
            long[] xxx = new long[f];
            try
            {
                if (f<0)
                {
                    throw new Exception("请输入大于0的整数!");
                }
                Stopwatch sw = new Stopwatch();
                sw.Start();
                for (int i = 0; i < f; i++)
                {
                    long x = FiBo(i);
                    xxx[i] = x;
                    mes += $"<div>第 {i} 项的值是:{x}</div>";
                }
                mes += $"<hr /><div>{string.Join(',',xxx)}</div>";
                sw.Stop();
                mes += $"<div>耗时:{sw.ElapsedMilliseconds} 毫秒</div>";
                return Content(mes);
            }
            catch (Exception ex)
            {
                return Content("出错:" + ex.Message);
            }
        }
        /// <summary>
        /// 算第n项的值
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        long FiBo(long n) {
            if (n == 0)
            {
                return 0;
            }
            else if (n == 1)
            {
                return 1;
            }
            else {
                return FiBo(n - 1) + FiBo(n - 2);
            }
        }