我在多行教科书中编写了一个代码来输出帕斯卡三角形。该程序适用于 1 到 12 之间的输入,但一旦输入 13 就会出现溢出错误。
我可以进行任何修改以使程序能够准确地为 13 及更高版本提供输出吗?
这是我使用的代码:
Public Class pascal_triangle
Private Function factorial(ByVal k As Integer) As Integer
If k = 0 Or k = 1 Then
Return 1
Else
Return k * factorial(k - 1)
End If
End Function
Private Sub BtnGen_Click(sender As Object, e As EventArgs) Handles BtnGen.Click
Dim nCr As Integer
Dim i, j, k As Integer
Dim output As String
output = ""
j = Val(TxtColumn.Text)
For k = 0 To j
For i = 0 To k
Dim fact, fact1, fact2 As Integer
fact = factorial(k)
fact1 = factorial(k - i)
fact2 = factorial(i)
nCr = fact / (fact1 * fact2)
TxtOutput.Text += Str(nCr) & output
Next
TxtOutput.Text += vbCrLf
Next
End Sub
End Class