VBでパスカルの三角形を作ろうとしています。
これが私のコードです(Grey's Theroyを使用しています):
Dim input As Integer = Val(TextBox1.Text)
Dim rownumber As Integer = 0
Dim columnumber As Integer = 1
Dim x As Integer = 1
Do Until rownumber = input
Do Until rownumber = columnumber
Label2.Text = 1
If rownumber = 0 Then
x = 1
Label2.Text = x & " "
Else
x = x * ((Math.Abs(rownumber - columnumber)) / (columnumber))
columnumber = columnumber + 1
Label2.Text = Label2.Text & " " & x & " "
End If
Loop
columnumber = 1
rownumber = rownumber + 1
Label2.Text = x & vbNewLine
Loop
別のアイデア、ここでは階乗を (関数を介して) 使用してから、行と列の値をインクリメントしています。出力は次のとおりです。
1
1 2
0 1 3
0 0 1 4
0 0 0 1 5
0 0 0 0 1 6
そしてコード:
Public Class Form1
Function factorial(ByVal n As Long)
If (n <= 0) Then
factorial = 1
Else
factorial = n * factorial(n - 1)
End If
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label2.Text = Nothing
Dim input As Integer = Val(TextBox1.Text)
Dim n As Integer
Dim r As Integer
Dim c As Integer
Dim rnum, cnum As Integer
For rnum = 0 To input Step +1
For cnum = 1 To (rnum + 1) Step +1
c = factorial(cnum) / (factorial(rnum) * factorial(cnum - rnum))
Label2.Text = Label2.Text & " " & c & " "
Next
Label2.Text = Label2.Text & vbCrLf
Next
End Sub
クラス終了