题目:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21….
由规律可知:
f(x) = f(x-1)+f(x-2)
Private Sub btnHare_Click(sender As Object, e As EventArgs) Handles btnHare.Click
'此算法实际是斐波拉契数列算法
Dim intMonth, intNum(), intI As Integer
Try
If IsNumeric(txtHare.Text) Then
intMonth = CInt(txtHare.Text)
If intMonth > 0 Then
lstHare.Items.Clear()
ReDim intNum(intMonth)
intNum(0) = 1
intNum(1) = 1
'计算繁殖数量
For intI = 2 To intMonth
intNum(intI) = intNum(intI - 2) + intNum(intI - 1)
Next
'列出结果
For intI = 0 To intMonth
lstHare.Items.Add(intI & "个月后兔子总数量是:" & intNum(intI))
Next
Else
MsgBox("不能小于0啊!要变回受精卵,还是要返古?")
End If
Else
MsgBox("错误,不是数字")
End If
Catch ex As Exception
MsgBox("错误,数字超出范围")
End Try
End Sub
本代码在VS2017+win10编译通过
程序下载
链接:https://pan.baidu.com/s/11a7v6K55XNDqaVAHNQ4AYg
提取码:mjc2