【问题标题】:Visual Basic biginteger giving me different solutionsVisual Basic biginteger 给我不同的解决方案
【发布时间】:2014-02-07 11:37:45
【问题描述】:

我正在尝试制作一个无限期地生成帕斯卡三角形行的程序。我使用的是整数、单数等,但不断出现溢出和“Infinity”值,所以我尝试使用 biginteger,现在它给了我不同的结果。我正在使用视觉工作室 2012 这是我前几个整数数据类型的输出: 1、 1 , 1 , 1 , 2 , 1 , 1 , 3 , 3 , 1 ,

这是我对 biginteger 数据类型的前几个输出: 1、 1 , 1 , 1 , 2 , 0 , 1 , 3 , 3 , 0 ,

我唯一改变的是数据类型。以下是相关位的代码:

    n = rows
    val(0) = 1
    For k = 1 To rows
        val(k) = val(k - 1) * (n / k)
        n -= 1
    Next

rows 是当前行(我在 timer_tick 上有这个,以便它可以无限期地运行,并且每个滴答行增加 1) 如何让 biginteger 数据类型返回与整数数据类型相同的值?

【问题讨论】:

    标签: vb.net visual-studio-2012 integer biginteger


    【解决方案1】:

    编辑,因为我意识到 stackoverflow 没有让你的换行符,整数是 32 位,而 bigint 是 64 位。当涉及更多数字时,分数可以在不同方向上四舍五入。

    为轻松避免舍入错误,您可以从代码中消除除法。

    例如)这里我们有一个运行字符串和临时字符串。我们拆分临时字符串以仅使用加法和减法计算下一行。

        Dim c As String = "1"
        Dim temp As New StringBuilder
        Dim f As New StringBuilder
        temp.Append(c)
        f.Append(temp.ToString)
        For n = 1 To 10
            Dim a() As String = c.Split(",")
            For x = LBound(a) To UBound(a)
                If UBound(a) - LBound(a) >= 1 Then
                    If x = UBound(a) Then
                        Dim v1 As UInt32 = a(x - 1)
                        Dim v2 As UInt32 = a(x)
                        temp.Append((v1) + (v2) & "," & 1)
                        c = temp.ToString
                        f.Append(vbCrLf & temp.ToString)
                        temp.Remove(0, temp.Length)
                    ElseIf x = LBound(a) Then
                        Dim v1 As UInt32 = a(x)
                        Dim v2 As UInt32 = a(x + 1)
                        temp.Append(1 & "," & (v1) + (v2) & ",")
                    ElseIf x < UBound(a) - 1 Then
                        Dim v1 As UInt32 = a(x)
                        Dim v2 As UInt32 = a(x + 1)
                        temp.Append((v1) + (v2) & ",")
                    End If
                Else
                    temp.Remove(0, temp.Length)
                    temp.Append("1,1")
                    f.Append(vbCrLf & temp.ToString)
                    temp.Remove(0, temp.Length)
                    temp.Append("1,2,1")
                    f.Append(vbCrLf & temp.ToString)
                    c = temp.ToString
                    temp.Remove(0, temp.Length)
                End If
            Next
        Next
            MsgBox(f.ToString)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多