输入行数n,显示具有n行的杨辉三角形.一个具有8行的杨辉三角形运行结果如图所示.
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/27 18:04:36
输入行数n,显示具有n行的杨辉三角形.一个具有8行的杨辉三角形运行结果如图所示.
输入行数n,显示具有n行的杨辉三角形.一个具有8行的杨辉三角形运行结果如图所示.
输入行数n,显示具有n行的杨辉三角形.一个具有8行的杨辉三角形运行结果如图所示.
Option Explicit
Dim n As Integer, i As Integer, j As Integer, a() As Integer
Private Sub Command1_Click()
'判断输入的行数是否为数字
If IsNumeric(Text1.Text) Then
n = Val(Text1.Text)
Else
MsgBox "您输入的不是数字"
Text1.SetFocus
End If
'重新定义数组并利用两次循环给数组赋值
ReDim a(n, n) As Integer
For i = 1 To n
a(i, 1) = 1: a(i, i) = 1
Next i
For i = 3 To n
For j = 2 To i - 1
a(i, j) = a(i - 1, j - 1) + a(i - 1, j)
Next j
Next i
'输出数组到picturebox1上
For i = 1 To n
For j = 1 To i
Picture1.Print a(i, j); Spc(1);
Next j
Picture1.Print
Next i
End Sub
Private Sub Command2_Click()
'判断输入的行数是否为数字
If IsNumeric(Text1.Text) Then
n = Val(Text1.Text)
Else
MsgBox "您输入的不是数字"
Text1.SetFocus
End If
'重新定义数组并利用两次循环给数组赋值
ReDim a(n, n) As Integer
For i = 1 To n
a(i, 1) = 1: a(i, i) = 1
Next i
For i = 3 To n
For j = 2 To i - 1
a(i, j) = a(i - 1, j - 1) + a(i - 1, j)
Next j
Next i
'输出数组到picturebox2上
For i = 1 To n
Picture2.Print Spc((n - i) * 2);
For j = 1 To i
Picture2.Print a(i, j); Spc(1);
Next j
Picture2.Print
Next i
End Sub
Private Sub Command3_Click()
'清除屏幕
Picture1.Cls
Picture2.Cls
End Sub
Private Sub Form_Load()
Text1.Text = ""
End Sub