VB中杨辉三角的问题

2024-11-25 12:31:02
推荐回答(1个)
回答1:

Private Sub C1_Click()
Dim A() As Long
Dim N As Integer
N = CInt(Val(T1.Text))
If N > 0 Then
ReDim A(1 To N, 1 To N) As Long
P1.Cls
For i = 1 To N
For j = 1 To i
If j = 1 Or j = i Then
A(i, j) = 1
Else
A(i, j) = A(i - 1, j) + A(i - 1, j - 1)
End If
P1.Print Tab(j * 6 - 5 + (N - i) * 3); A(i, j);
Next
Next

Else
MsgBox "请重新输入一个有效的行数:"
T1 = ""
T1.SetFocus
End If
End Sub