'100位數的大數相加
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim v1 = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
Dim v2 = "13579086421357908642135790864213579086421357908642135790864213579086421357908642135790864213579086421357908642135790864213579086421357908642"
Dim a(100) As Integer
Dim b(100) As Integer
Dim c(100) As Integer
Dim d(100) As Integer
'把代表大數的字串的每個數字,放入陣列中,a(1)代表個位數,a(2)代表十位數,餘類推
For i = 1 To 100
a(i) = Val(Mid(v1, 100 + 1 - i, 1))
b(i) = Val(Mid(v2, 100 + 1 - i, 1))
Next
'從個位數加起
c(0) = 0 '個位數,没有前一位進位狀況,設為0
For i = 1 To 100
d(i) = a(i) + b(i) + c(i - 1)
c(i) = d(i) \ 10
d(i) = d(i) Mod 10
Next
'輸出結果
Dim rStr = ""
If c(100) > 0 Then rStr = rStr & CStr(c(100))
For i = 100 To 1 Step -1
rStr = rStr & CStr(d(i))
Next
MsgBox(v1 & " + " & v2 & " = " & rStr)
End
End Sub
End Class
同學可先試試10位數的加、減、乘、除, 再推進到100位數,最後再改為1-100中的任何位數均可處理。
目前還做不出來也不用灰心,畢竟這是一般資訊相關科系,大二、大三資料結構的練習題目。
沒有留言:
張貼留言