斐波那契数列如何快速计算-动态规划法
【斐波那契数列如何快速计算-动态规划法】下面的代码使用C#写的:
class Program
{
static void Main(string[] args)
{
Program a = new Program();
DateTime strat = DateTime.Now;
long kkkk = a.Fibnacci(10);
TimeSpan s = DateTime.Now - strat;
Console.WriteLine(s.TotalMilliseconds + " = " + kkkk);
strat = DateTime.Now;
List cc = new List();
kkkk = a.Fibnacci2(50, cc);
s = DateTime.Now - strat;
Console.WriteLine(s.TotalMilliseconds + " = " + kkkk);
Console.ReadKey();
}long Fibnacci(long n) {
if (n == 0 || n == 1)
{
return 1;
}
else
{
return this.Fibnacci(n - 1) + this.Fibnacci(n - 2);
}
}long Fibnacci2(long n, List k)//动态规划法
{
if (n == 0 || n == 1)
{
return 1;
}
else
{
if (n - 2 >= k.Count)
{
k.Add(this.Fibnacci2(n - 1 , k) + this.Fibnacci2(n - 2, k));
}return k[(int)n -2];
}
}
}
推荐阅读
- Python基础|Python基础 - 练习1
- [Golang]力扣Leetcode—初级算法—动态规划—爬楼梯(斐波那契数列)
- 【golang】leetcode初级-实现|【golang】leetcode初级-实现 strStr()&外观数列
- 每日一练(5)(斐波那契数列)
- 笔试面试|网易笔试(等差数列)
- 题库-CF|【Codeforces Round 370 (Div 2) E】【线段树 等比数列 区间合并】Memory and Casinos 赌场区间[l,r] l进r先出的概率
- Python|Python Leetcode(665.非递减数列)
- 牛客挑战赛39 C 牛牛的等差数列(线段树)(*)
- C/C++程序|C程序-牛客网-等差数列
- Python练习题(九九乘法表、打印菱形、求素数、求斐波那契数列)