我想计算Visual Basic中的二项式系数,但我只能做分子部分,因为我在分母部分丢失了。我正在使用的等式如下图所示。
看看C(n,k)方程,我只能做n因子的分子。我在分母方面遇到了麻烦。我的分子代码(n!)如下:
Dim nfactorial As Integer = 1
For i As Integer = 1 To txt3.Text
nfactorial *= i
Next
关于分母部分我该怎么办?谢谢!
答案 0 :(得分:0)
您不能单独计算分子和分母。您是否尝试在.NET中查找(30!),尤其是使用Integer
?
这是作业吗?无论如何,只需看看以下帖子:http://csharphelper.com/blog/2014/08/calculate-the-binomial-coefficient-n-choose-k-efficiently-in-c/
vb中的代码:
Dim result As Decimal = 1
For i As Integer = 1 To K
result *= N - (K - i)
result /= i
Next
Return result
答案 1 :(得分:0)
试试这个:
'by example: C(5,3) = 10
Dim n, k As Integer
Dim result As String = ""
n = txt3.Text
For k = 0 To 3
result = result & "For C(" & n & "," & k & ")=" & C(n, k) & vbCrLf
Next
MsgBox(result)
.
.
.
Private Function C(n As Integer, k As Integer)
Dim result As Decimal = 1
For i As Integer = 1 To k
result *= n - (k - i)
result /= i
Next
Return result
End Function
编辑:键盘输入的k=0 to 3
和n
。