2012年8月20日 星期一

簡易大數乘法

輸入2個數 N1、N2 。 (0 < N1)、(0 < N2 < 100)

求出N1與N2的乘積。



輸入:
100000000000000
99
輸出:

9900000000000000

1 則留言:

  1. 發現遺漏了這題!!
    Private Sub Form_Load()
    Me.Hide
    Dim a(3)
    Dim e, d, c As Integer
    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
    Do
    If Right(x, 1) = 0 Then ans = ans & "0": x = Val(x) / 10
    If Right(y, 1) = 0 Then ans = ans & "0": y = Val(y) / 10
    Loop Until Right(x, 1) <> 0 And Right(y, 1) <> 0
    t = 0
    For j = Len(y) To 1 Step -1
    t = t + 1
    d = 0
    For i = Len(x) To 1 Step -1
    r = (Mid(y, j, 1) * Mid(x, i, 1)) + d
    d = r \ 10
    If i <> 1 Then c = r Mod 10
    If i = 1 Then c = r
    a(t) = c & a(t)
    Next
    For k = 1 To t
    a(t) = a(t) & "0"
    Next
    a(t) = Mid(a(t), 1, Len(a(t)) - 1)
    Next
    Do
    If Len(a(1)) <> Len(a(2)) Then
    If Len(a(1)) > Len(a(2)) Then
    a(2) = "0" & a(2)
    Else
    a(1) = "0" & a(1)
    End If
    End If
    Loop Until Len(a(2)) = Len(a(1))

    If t = 2 Then
    d = 0
    For i = Len(a(1)) To 1 Step -1
    e = Val(Mid(a(1), i, 1)) + Val(Mid(a(2), i, 1)) + d
    d = e \ 10
    If i <> 1 Then c = e Mod 10
    If i = 1 Then c = e
    ans = c & ans
    Next
    Else
    ans = a(1) & ans
    End If
    Print #2, ans
    Close
    Close
    End
    End Sub

    回覆刪除