VB编程打印n层金字塔行杨辉三角

来源:百度知道 编辑:UC知道 时间:2024/09/22 19:39:58
1
1 1
1 2 1
1 3 3 1

Private Sub Form_Click()
Const N = 10
Dim a(N, N) As Integer
Dim i, j As Integer
a(0, 0) = 1
a(1, 0) = 1
a(1, 1) = 1
For i = 2 To N - 1
a(i, 0) = 1
a(i, i) = 1
For j = 1 To i - 1
a(i, j) = a(i - 1, j - 1) + a(i - 1, j)
Next j, i

For i = 0 To N - 1
For j = 0 To i
Print a(i, j);

Next j

Print

Next i

End Sub

基于上面的回答,加了空格,看起来更像金字塔

Private Sub Form_Click()
Const N = 10
Dim a(N, N) As Integer
Dim i, j As Integer
a(1, 1) = 1
a(2, 1) = 1
a(2, 2) = 1
For i = 3 To N
a(i, 1) = 1
a(i, i) = 1
For j = 2 To i - 1
a(i, j) = a(i - 1, j - 1) + a(i - 1, j)
Next j, i

For i = 1 To N
Print Space(2 * N - 2 * i);
For j = 1 To i - 1
Print a(i, j);

Next j

Print

Next i

End Sub