1个回答
展开全部
'递归函数
Public Function fb(n As Integer) As Single
If n > 2 Then
fb = fb(n - 1) + fb(n - 2)
Else
If n = 2 Then
fb = 1
Else
If n = 1 Then
fb = 1
Else
fb = 0
End If
End If
End If
End Function
'非递归
Dim a As Single, b As Single, c As Single, n As Integer
a = 1
b = 1
n = Val(InputBox("输入n"))
'打印递归函数值
Print fb(n)
’以下为非递归算法
If n > 2 Then
For i = 3 To n Step 1
c = a + b
a = b
b = c
Next i
Else
If n = 1 Then
c = 1
Else
If n = 2 Then
c = 1
End If
End If
End If
Print c
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询