日期:2014-05-18  浏览次数:20661 次

递归算法
有一个队列:1   ,1,2,   3,   5,   8   ,13,   21,   34…….求第30个数是多少?

------解决方案--------------------
Answer = Test(30) = 832040

Private Function Test(ByVal i As Integer) As Integer

If i = 1 OrElse i = 2 Then
Return 1
Else
Return Test(i - 2) + Test(i - 1)
End If

End Function

------解决方案--------------------
这个比较简单的哦!
算法的结构:要求的数 ==(等于)要求的数的前第一个数+要求的数的前第二个数
下面的为我写的控制台应用程序(c#)

public class MainClass
{
public static void Main()
{
Console.WriteLine(haha(30));
}
public static int haha(int i)
{
if(i <=0)
{
return 0;
}
else if(i> 0 &&i <=2)
{
return 1;
}
else
{
haha(i-1)+haha(i-2);
}

}
}