动态规划初识

/// <summary>
/// 动态规划的思想,计算斐波那契数列。只计算到第5个数
/// </summary>
public class MyFib
{
//记录已经计算过的数
int[] m = new int[5];
bool[] bn = new bool[5];

public MyFib()
{
m[0]=m[1]=1;
bn[0]=bn[1]=true;
}

public int F(int t)
{
try
{
//检查该数是否计算过
while (!bn[t])
{
m[t] = F(t - 1) + F(t - 2);
bn[t] = true;
}
return m[t];
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return -1;
}
}
}

posted @ 2013-06-17 10:12  viola  阅读(201)  评论(0编辑  收藏  举报