VERSION 5.00
Begin VB.Form Form1
AutoRedraw = -1 'True
Caption = "打印杨辉三角"
ClientHeight = 8220
ClientLeft = 60
ClientTop = 450
ClientWidth = 10995
LinkTopic = "Form1"
ScaleHeight = 8220
ScaleWidth = 10995
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Print1
Caption = "打印杨辉三角"
Height = 375
Left = 8880
TabIndex = 2
Top = 120
Width = 1215
End
Begin VB.TextBox Text
Height = 375
Left = 7320
TabIndex = 1
Top = 120
Width = 1335
End
Begin VB.Label Label1
Caption = "输入N:"
Height = 255
Left = 6120
TabIndex = 0
Top = 240
Width = 735
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Print1_Click()
Dim i, j As Integer
Dim n As Integer
Dim a(40, 40) As Integer
Dim str As String
str = ""
n = Text.Text
Print "N的值不大于15!"
If n <= 0 Or n > 40 Then
Print "输入的数不合法!请重新输入"
Text.Text = ""
Text.SetFocus
End If
Print1.SetFocus
For i = 0 To n '初始化
For j = 0 To n + i + 1
a(i, j) = 0
Next
a(i, n - i) = 1
a(i, n + i) = 1
Next
For i = 2 To n
For j = 1 To n + i
a(i, j) = a(i - 1, j - 1) + a(i - 1, j + 1)
Next
Next
Print " 打印杨辉三角"
For i = 0 To n
For j = 0 To n + i
If a(i, j) = 0 Then
str = str + " "
End If
If a(i, j) = 1 Then
str = str + "1 "
End If
If a(i, j) > 1 And a(i, j) < 10 Then
str = str + CStr(a(i, j)) + " "
End If
If a(i, j) >= 10 And a(i, j) < 100 Then
str = str + CStr(a(i, j)) + " "
End If
If a(i, j) >= 100 Then
str = str + CStr(a(i, j)) + " "
End If
Next
Print str
Print
str = ""
Next
End Sub
评论