窗體上添加一個按鈕,代碼如下:
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比靈丘網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式靈丘網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋靈丘地區(qū)。費(fèi)用合理售后完善,十余年實體公司更值得信賴。
Option Explicit
Private Sub Command1_Click()
Dim N As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
N = InputBox("請輸入行數(shù)!", , 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) '讓數(shù)組中間的元素等于此元素所在序號的步長加1
注意如上語句,以及循環(huán)條件:
For i = 1 To n
For j = 1 To i
因此可以預(yù)見到,當(dāng)i取n的時候,j也取i(n)的時候,a(i+1, j+1)超過了a(n,n),因此為了算法的簡單(去掉不是很必要的判斷語句),把n范圍擴(kuò)大
先把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的值加大 當(dāng)n》16的時候就顯示“參數(shù)“Number”必須大于或等于 0?!绷?有沒有辦法解決?,