Option Explicit
Dim F值() As Double
Public Function 阶乘(cs As Long) As Double
'n!
If cs = 1 Then
阶乘 = 1
Else
阶乘 = cs * 阶乘(cs - 1)
End If
End Function
Private Sub Command1_Click()
Dim n As Long
n = Val(Text1.Text)
ReDim F值(n)
F值(1) = 1
'F(1) = F(1) * F(1 - 1)
'F(2) = F(1) * F(2 - 1) + F(2) * F(2 - 2)
'F(3) = F(1) * F(3 - 1) + F(2) * F(3 - 2) + F(3) * F(3 - 3)
'F(4) = F(1) * F(4 - 1) + F(2) * F(4 - 2) + F(3) * F(4 - 3) + F(4) * F(4 - 4)
'F(5) = F(1) * F(5 - 1) + F(2) * F(5 - 2) + F(3) * F(5 - 3) + F(4) * F(5 - 4) + F(5) * F(5 - 5)
Dim i As Long, j As Long
For i = 2 To n
F值(i) = F值(i - 1)
For j = 1 To i
F值(i) = F值(i) + F值(j) * F值(i - j)
Next j
Next i
'P(n)=n!×(∑F(k)×F(n-k))
Dim p As Double
p = 阶乘(n)
j = 0
For i = 1 To n - 1
j = j + F值(i) * F值(n - i)
Next i
p = p * j
Label1.Caption = p
End Sub
---------------------------------
不知道是这有这样的, 关键在 F 的计算上面.
F 的计算不能用递归,必须要有 变通方法,否则就会因数据稍大就造成 堆栈 溢出.
--------------------