求助vb高手--分别采用递归法和递推法求斐波纳契数列
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/23 03:12:19
求助vb高手--分别采用递归法和递推法求斐波纳契数列求助vb高手--分别采用递归法和递推法求斐波纳契数列求助vb高手--分别采用递归法和递推法求斐波纳契数列递归法和循环法分别如下:PrivateFun
求助vb高手--分别采用递归法和递推法求斐波纳契数列
求助vb高手--分别采用递归法和递推法求斐波纳契数列
求助vb高手--分别采用递归法和递推法求斐波纳契数列
递归法和循环法分别如下:Private Function Fibonacci1(n As Integer) As Long Dim a As Integer,b As Integer If n = 1 Or n = 2 Then Fibonacci1 = 1 Else Fibonacci1 = Fibonacci1(n - 1) + Fibonacci1(n - 2) End If End Function Private Function Fibonacci2(n As Integer) As Long Dim a As Long,b As Long a = 1 b = 1 If n = 1 Or n = 2 Then Fibonacci2 = 1 Else For i = 3 To n Fibonacci2 = a + b a = b b = Fibonacci2 Next i End If End Function Private Sub Form_Click() Print Fibonacci1(5) Print Fibonacci2(5) End Sub 运行结果都是5.