您的当前位置:首页正文

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

2008-04-12 来源:赴品旅游

有网友碰到这样的问题“VB编程打印n层金字塔行杨辉三角”。小编为您整理了以下解决方案,希望对您有帮助:

解决方案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

解决方案2:

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

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

显示全文