十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
窗体上添加一个按钮,代码如下:
创新互联-专业网站定制、快速模板网站建设、高性价比灵丘网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式灵丘网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖灵丘地区。费用合理售后完善,十余年实体公司更值得信赖。
Option Explicit
Private Sub Command1_Click()
Dim N As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
N = InputBox("请输入行数!", , 10)
ReDim a(N + 1, N + 1), b(N + 1, N + 1)
Cls
k = 8
For i = 1 To N
Print String((N - i) * k / 2 + 1, " ");
For j = 1 To i
a(i, 1) = 1
a(i, i) = 1
a(i + 1, j + 1) = a(i, j) + a(i, j + 1)
b(i, j) = Trim(Str(a(i, j)))
Print b(i, j); String(k - Len(b(i, j)), " ");
Next j
Next i
End Sub
a(i + 1, j + 1) = a(i, j) + a(i, j + 1) '让数组中间的元素等于此元素所在序号的步长加1
注意如上语句,以及循环条件:
For i = 1 To n
For j = 1 To i
因此可以预见到,当i取n的时候,j也取i(n)的时候,a(i+1, j+1)超过了a(n,n),因此为了算法的简单(去掉不是很必要的判断语句),把n范围扩大
先把form的autoredraw属性改成true,代码如下:
==========================
Option Explicit
Dim a(10, 10) As Long
Dim b(10) As String
Dim i As Byte
Dim j As Byte
Private Sub Form_Load()
a(0, 0) = 1
For i = 1 To 9
For j = 1 To 9
a(i, j) = a(i - 1, j - 1) + a(i - 1, j)
If a(i, j) 0 Then
b(j) = b(j - 1) " " a(i, j)
End If
Next
Print b(i)
Next
End Sub
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim n As Integer, i As Integer, j As Integer, a(,) As Integer
n = 10
ReDim a(n + 1, n + 1)
For i = 1 To n + 1
a(i, 1) = 1 : a(i, i) = 1 : Next i
For i = 3 To n + 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 + 1
For j = 1 To i
TextBox1.AppendText(Space(4 - Len(Trim(Str(a(i, j))))) Trim(Str(a(i, j))))
Next j
TextBox1.AppendText(vbCrLf)
Next i
End Sub
End Class,9,
xixihahano1 举报
谢谢了,但是你写的代码可以输出多少行?多久会溢出了
举报 ccddty
没试过,你可以试试,将n的值加大 当n》16的时候就显示“参数“Number”必须大于或等于 0。”了 有没有办法解决?,