2011年3月11日 星期五

簡易大數加法

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


輸入:

9999999999999999999999999999999999999999999999999999999999999999999999999999
1000


輸出:

10000000000000000000000000000000000000000000000000000000000000000000000000999

11 則留言:

  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

    Dim N1 As String, N2 As String, B As String, NS1 As Integer, NS2 As Integer, PLUS, tmp
    Input #1, N1
    Input #1, N2





    Do Until Len(N1) = Len(N2)
    If Len(N1) > Len(N2) Then
    N2 = "0" & N2
    Else
    N1 = "0" & N1
    End If
    Loop
    If N1 > N2 Then B = N1 Else B = N2
    tmp = 0

    For i = Len(B) To 1 Step -1

    NS1 = Mid(N1, i, 1)
    NS2 = Mid(N2, i, 1)

    L = NS1 + NS2

    If L + tmp <= 9 Then ans = (L + tmp) & ans Else ans = (L + tmp) Mod 10 & ans: tmp = (L + tmp) \ 10
    If i = 1 And L + tmp > 9 Then ans = "1" & ans
    Next

    Print #2, ans

    Close
    Close

    End
    End Sub

    //----------

    大數的複雜加法真的蠻難驗算的呢

    回覆刪除
  2. arro好,
    1.程式OK。
    2.
    IIf L + tmp <= 9 Then ans = (L + tmp) & ans Else ans = (L + tmp) Mod 10 & ans: tmp = (L + tmp) \ 10
    這個判斷,也是不必要的吧,反正就用後頭的
    ans = (L + tmp) Mod 10 & ans: tmp = (L + tmp) \ 10

    3.還有,不同的運算之間,多利用括弧來確定想要的運算優先順序。
    ans = ((L + tmp) Mod 10) & ans
    會比較安心些。

    4.前頭的將兩個字串做成一樣長的方式,滿好的。

    5.驗算的部分,可以用亂數產生幾個長整數範圍內的數,這樣子可以用正常的加法去檢查答案是否相同。可以多產生幾組。

    回覆刪除
  3. Private Sub Form_Load()
    Me.Hide
    Dim a As String
    Dim m As String
    Dim n As String
    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1
    Input #1, m, n
    i = 0
    t = 0
    'Do While Len(m) > i And Len(n) > i 它是錯的
    Do Until i > Len(m) And i > Len(n)
    'Do While Len(m) + 1 >= i And Len(n) + 1 >= i 它是錯的
    If Len(m) > i Then x = Val(Mid(m, Len(m) - i, 1)) Else x = 0
    If Len(n) > i Then y = Val(Mid(n, Len(n) - i, 1)) Else y = 0
    z = x + y + c
    c = z \ 10
    a = (z Mod 10) & a
    i = i + 1
    Loop
    Print #2, a
    Close #1
    Close #2
    End
    End Sub
    ----------------------
    好吧 今早才知道until是"直到" -.-
    仍然不了解那三行的差別~"~

    回覆刪除
  4. 緣尉好,
    邏輯運算中的迪摩根定律:
    (x+y)'=x'‧y'
    (x.y)' = x' + y'

    not(x or y) = not x and not y
    not(x and y) = not x or not y

    不理解的話,見面再說。

    回覆刪除
  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
    Dim a As String, b As String
    Input #1, a
    Input #1, b
    Print #2, Bplus(a, b)

    Close
    Close
    End
    End Sub
    Function Bplus(N1 As String, N2 As String)
    Dim NS1 As Integer, NS2 As Integer, Tmp As Integer, Plus As String, FAns As String

    Do Until Len(N1) = Len(N2)
    If Len(N1) > Len(N2) Then
    N2 = "0" & N2
    Else
    N1 = "0" & N1
    End If
    Loop


    For i = Len(N1) To 0 Step -1

    If i = 0 Then
    FAns = Tmp & FAns
    Else
    NS1 = Mid(N1, i, 1)
    NS2 = Mid(N2, i, 1)
    Plus = NS1 + NS2
    FAns = ((Plus + Tmp) Mod 10) & FAns
    Tmp = (Plus + Tmp) \ 10
    End If

    Next

    If Left(FAns, 1) = "0" Then FAns = Right(FAns, Len(FAns) - 1)

    Bplus = FAns

    End Function


    //-----------

    大數加法的函數型~

    回覆刪除
  6. Dim m As String
    Dim n As String
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1
    Input #1, e, f, g
    Print #2, p(e, f)
    Close #1
    Close #2
    End
    End Sub
    Function p(m, n)
    Lenm = Len(m)
    Lenn = Len(n)
    Do Until i > Lenm And i > Lenn
    If Lenm > i Then x = Val(Mid(m, Lenm - i, 1)) Else x = 0
    If Lenn > i Then y = Val(Mid(n, Lenn - i, 1)) Else y = 0
    z = x + y + c
    c = z \ 10
    a = (z Mod 10) & a
    i = i + 1
    If i > Lenm And i > Lenn And (z Mod 10 = 0) Then a = Right(a, Len(a) - 1)
    Loop
    p = a
    End Function
    ---------------------
    最後loop那行算是硬性去掉0吧= =
    為什麼會多0咧

    回覆刪除
  7. 緣尉好,
    這樣子程式也正確。
    但是,你改著改著,還是忘了題目原來的定義了。
    人家題目是兩個數。

    至於為什麼多個0,是不是在迴圈的地方,多執行一次呢。

    回覆刪除
  8. Private Sub Form_Load()
    Me.Hide
    Dim A As String, B As String
    Dim ans As String
    Dim X
    Dim go
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2

    Input #1, A, B

    If Len(B) > Len(A) Then
    C = B
    B = A
    A = C
    End If

    For i = 1 To Len(A) - Len(B)
    B = "0" & B
    Next i

    go = 0
    ans = ""

    For i = Len(A) To 1 Step -1
    X = Val(Mid(A, i, 1)) + Val(Mid(B, i, 1)) + go
    go = 0
    If X >= 10 Then go = X \ 10: X = X Mod 10
    ans = CStr(X) & ans
    If i = 1 And go <> 0 Then ans = CStr(go) & ans
    Next i

    Print #2, ans

    Close #1
    Close #2
    End
    End Sub

    回覆刪除
  9. 佑好,
    這一小陣子,你的進度,小小停了一下的感覺,要加油哦。
    這題這樣子可以。
    你也一樣有個小地方沒想到,
    If i = 1 And go <> 0 Then ans = CStr(go) & ans
    這一行,不用放在迴圈裡,每次都要檢查一次吧。
    直接放在迴圈後,看go是不是還有進位的,如果有,去接上就好了。
    放在迴圈中,多執行很多次,沒有錯誤,但是,速度變慢了。
    浪費是種罪惡啊,小說裡都這麼說的。

    回覆刪除
  10. Dim N1 As Long
    Dim N2 As Long
    Dim ans As Long
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, N1, N2

    ans = N((N1), (N2))

    Print #2, "最大公因數" & ans
    Print #2, "最小公倍數" & (N1 * N2) / ans

    Close #2
    Close #1



    End
    End Sub

    Function N(A, B)
    Do
    If A > B Then
    A = A - B
    Else
    B = B - A
    End If
    Loop Until A = B
    N = A
    End Function

    回覆刪除
  11. 放錯地方的最大公因數最小公倍數吧。

    回覆刪除