使用vb打印杨辉三角?
1个回答
展开全部
Private Sub Command1_Click()
Dim a() As Long
Dim n As Integer, v
Dim i As Integer, j As Integer
v = InputBox("请输入杨辉三角的行数")
If IsNumeric(v) = False Or Len(v) = 0 Then
MsgBox ("输入不是数字或者为空,请重新输入!")
Else
n = v
End If
ReDim a(n, n)
For i = 1 To n
For j = 1 To i
If j = 1 Or i = j Then
a(i, j) = 1
Else
a(i, j) = a(i - 1, j - 1) + a(i - 1, j)
End If
Next j
Next i
For i = 1 To n
For j = 1 To i
Print Tab(n * 3 - 3 * i + 6 * j); a(i, j);
Next j
Next i
End Sub
杨辉三角的另一个方法
Private Sub Command1_Click()
Dim a() As Integer
Dim b() As Integer
Dim i As Integer
Dim j As Integer
ReDim b(1)
b(1) = 1
For i = 1 To 10
ReDim a(i)
ReDim Preserve b(i)
Print Space(11 - i);
For j = 1 To i
a(j) = b(j - 1) + b(j)
Print a(j);
Next j
For j = 1 To i
b(j) = a(j)
Next j
Next i
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询