帕斯卡';s三角形-VB.NET

帕斯卡';s三角形-VB.NET,vb.net,Vb.net,我已经创建了一个程序,该程序将显示x行数,并重复该操作: i、 e 现在我想做一个帕斯卡三角形,可能是这样的: Dim arr As Integer(,) = New Integer(7, 7) {} For i As Integer = 0 To 7 For k As Integer = 7 To i + 1 Step -1 'print spaces Console.Write(" ") Next For j As Integer

我已经创建了一个程序,该程序将显示x行数,并重复该操作:

i、 e


现在我想做一个帕斯卡三角形,可能是这样的:

Dim arr As Integer(,) = New Integer(7, 7) {}
 For i As Integer = 0 To 7
    For k As Integer = 7 To i + 1 Step -1
        'print spaces
        Console.Write(" ")
    Next

    For j As Integer = 0 To i - 1
        If j = 0 OrElse i = j Then
            arr(i, j) = 1
        Else
            arr(i, j) = arr(i - 1, j) + arr(i - 1, j - 1)
        End If
        Console.Write(arr(i, j) & " ")
    Next
    Console.WriteLine()
Next
控制台输出:


另一种方法,只在内存中保留以前和当前的迭代:

Dim oldList As New List(Of Integer)({0, 1})
For line = 1 To 7
  Dim newList As New List(Of Integer)
  For i = 1 To oldList.Count - 1
    newList.Add(oldList(i - 1) + oldList(i))
  Next
  Debug.Print(String.Join(" ", newList))
  oldList.Clear()
  oldList.Add(0)
  oldList.AddRange(newList)
  oldList.Add(0)
Next

要使用windows窗体执行此操作,您需要一个文本框、多行文本框和设计界面上的一个按钮

下面是生成它所需的代码

Imports System.Numerics 'this allows you to  use big integer

Public Class pascal_triangle


    Private Function factorial(ByVal k As Integer) As BigInteger

'big integer allows your proram compute for inputs of more than 22

        If k = 0 Or k = 1 Then

            Return 1

        Else

            Return k * factorial(k - 1)


        End If

    End Function



    Private Sub BtnGen_Click(sender As Object, e As EventArgs) Handles BtnGen.Click
        Dim nCr As Double

        Dim i, j, k As Integer


        Dim output As String

        output = ""

        j = Val(TxtColumn.Text)

        For k = 0 To j

            For i = 0 To k

                Dim fact, fact1, fact2 As BigInteger



                fact = factorial(k)

                fact1 = factorial(k - i)

                fact2 = factorial(i)

                nCr = fact / (fact1 * fact2)

                TxtOutput.Text += Str(nCr) & output





            Next

            TxtOutput.Text += vbCrLf

        Next



    End Sub


    Private Sub pascal_triangle_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class

好的,我在一个控制台应用程序中实现了这一点,但是我想让它成为一种表单样式,用户输入行号。我现在正在自学VB,因此我试图理解这个例子。。你能简单解释一下它的工作原理吗?谢谢!我在上面添加了一些新代码,如果您能看一下,我会理解得更好。ThanksHow我会将其输出到标签,以及如何使用户输入的行数变为变量?我在上面添加了一些新代码,如果您能看一下,我会理解得更好。Thanks@DavidSalib:关于标签的输出,请先收集到StringBuilder中,然后将其转储到标签中,如下所示
label.Text=sb.ToString()
。因此,您可以将
Debug.Print(String.Join(“,newList))
替换为
sb.AppendLine(String.Join(“,newList))
。7是要计算的行数,将其替换为CInt(yourInput.Text),并且不要忘记错误检查。
Imports System.Numerics 'this allows you to  use big integer

Public Class pascal_triangle


    Private Function factorial(ByVal k As Integer) As BigInteger

'big integer allows your proram compute for inputs of more than 22

        If k = 0 Or k = 1 Then

            Return 1

        Else

            Return k * factorial(k - 1)


        End If

    End Function



    Private Sub BtnGen_Click(sender As Object, e As EventArgs) Handles BtnGen.Click
        Dim nCr As Double

        Dim i, j, k As Integer


        Dim output As String

        output = ""

        j = Val(TxtColumn.Text)

        For k = 0 To j

            For i = 0 To k

                Dim fact, fact1, fact2 As BigInteger



                fact = factorial(k)

                fact1 = factorial(k - i)

                fact2 = factorial(i)

                nCr = fact / (fact1 * fact2)

                TxtOutput.Text += Str(nCr) & output





            Next

            TxtOutput.Text += vbCrLf

        Next



    End Sub


    Private Sub pascal_triangle_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class