2012年2月25日 星期六

簡易大數加法

求兩筆數字N的和。 (N為正整數,長度不限)



輸入:

9999999999999999999999999999999999999999999999999999999999999999999999999999
1000


輸出:

10000000000000000000000000000000000000000000000000000000000000000000000000999

7 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Line Input #1, x
    Line Input #1, y
    a1 = Val(Len(x))
    a2 = Val(Len(y))
    Do
    If a1 > a2 Then
    y = "0" & y
    Else
    x = "0" & x
    End If
    a1 = Val(Len(x))
    a2 = Val(Len(y))
    Loop Until a1 = a2
    For j = a1 To 1 Step -1
    c = Val(Mid(x, j, 1)) + Val(Mid(y, j, 1)) + d
    If j = 1 And c >= 10 Then ans = c & ans: Exit For
    If c >= 10 Then
    d = Val(c \ 10)
    e = Val(c Mod 10)
    ans = e & ans
    Else
    ans = c & ans
    End If
    Next
    Print #2, ans
    Close
    Close
    End
    End Sub

    回覆刪除
  2. 小冰好,
    程式正確。
    len()出來的值,就是數字了。val()有點兒多餘。
    哦,好像有錯。
    d的值,沒有歸0,會不會有多加的狀況呢?
    其實,不管c有沒有大於10,還是做d=c \ 10 , e = c mod 10
    那麼,每次都是ans = e & ans

    回覆刪除
  3. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Line Input #1, x
    Line Input #1, y
    a1 = Len(x)
    a2 = Len(y)
    Do
    If a1 > a2 Then
    y = "0" & y
    Else
    x = "0" & x
    End If
    a1 = Len(x)
    a2 = Len(y)
    Loop Until a1 = a2
    For j = a1 To 1 Step -1
    d = 0
    c = Val(Mid(x, j, 1)) + Val(Mid(y, j, 1)) + d
    If j = 1 And c >= 10 Then ans = c & ans: Exit For
    d = Val(c \ 10)
    e = Val(c Mod 10)
    ans = e & ans
    Next
    Print #2, ans
    Close
    Close
    End
    End Sub

    d值 那時候沒有發現,所以可能下次要多作幾組的測試了。
    因為那時候沒有設定val(),所以程式就有些錯誤,就把全部有關的數字都改成有val()了,不過下次會注意的。

    回覆刪除
  4. 小冰好,
    這一改,錯的更直接了吧。
    d = 0
    c = Val(Mid(x, j, 1)) + Val(Mid(y, j, 1)) + d
    在加d之前,就歸0,那就不用加了啊。

    就學起來就是了,(你寫下次會注意,聽起來似乎熊掌在訓話似的,沒那麼嚴重啦。):-)

    到目前為止,你做的很好,繼續前進,將時間放進去,功力就累積出來了哦。

    回覆刪除
  5. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Line Input #1, x
    Line Input #1, y
    a1 = Len(x)
    a2 = Len(y)
    Do
    If a1 > a2 Then
    y = "0" & y
    Else
    x = "0" & x
    End If
    a1 = Len(x)
    a2 = Len(y)
    Loop Until a1 = a2
    For j = a1 To 1 Step -1
    c = Val(Mid(x, j, 1)) + Val(Mid(y, j, 1)) + d
    If j = 1 And c >= 10 Then ans = c & ans: Exit For
    d = Val(c \ 10)
    e = Val(c Mod 10)
    ans = e & ans
    Next
    Print #2, ans
    Close
    Close
    End
    End Sub

    早上精神有點不太好,所以就少想了每一次都先歸0在加d,也是一樣想法,每一次跑完d就重新計算,應該不會在有多加少加的問題了。
    這是習慣了,常常因為稍微不注意就錯誤了,所以有錯了,下次就會記得更清楚了。

    回覆刪除
  6. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Dim c As Integer

    Line Input #1, s1
    Line Input #1, s2
    a = Len(s1)
    b = Len(s2)
    Do
    If a > b Then
    s2 = 0 & s2
    Else
    s1 = 0 & s1
    End If
    a = Len(s1)
    b = Len(s2)
    Loop Until a = b

    d = 0
    For j = a To 1 Step -1
    c = Val(Mid(s1, j, 1)) + Val(Mid(s2, j, 1)) + d
    d = c \ 10
    x = c Mod 10
    ans = x & ans
    Next j
    If d > 0 Then ans = d & ans
    Print #2, ans
    Close
    Close
    End
    End Sub

    回覆刪除
  7. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Line Input #1, x
    Input #1, y
    a1 = Len(x)
    a2 = Len(y)
    Do
    If a1 > a2 Then
    y = 0 & y
    Else
    x = 0 & x
    End If
    a1 = Len(x)
    a2 = Len(y)
    Loop Until a1 = a2
    d = 0
    For i = a1 To 1 Step -1
    c = Val(Mid(x, i, 1)) + Val(Mid(y, i, 1)) + d
    d = c \ 10
    k = c Mod 10
    ans = k & ans
    Next i
    If d > 0 Then ans = d & ans
    Print #2, ans
    Close #2
    Close #1
    End
    End Sub

    回覆刪除