Problem7:進位判斷(13%)

Solomon posted @ 2010年8月14日 17:46 in 98程式設計模擬試題 , 1153 阅读

加法的運算是把 2 個整數靠右對齊,然後由右至左,一位一位相加。如果相加的結果大

於等於 10 就有進位(carry)發生。請寫個程式來判斷兩個正整數相加時,產生了幾次進位的
情況。 
 
輸入說明: 
第一行的數字,表示有幾組測試資料,第二行開始的每一行即為一筆測試資料。每一行
輸入的資料有兩個正整數,以一個空格分開,每個整數的長度均小於 100 位數。 
 
輸出說明: 
對每一筆測試資料,輸出相加後有幾次進位的次數。 
 
輸入範例: 
123 456 
555 555 
 
輸出範例: 
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim fileContents As String
        fileContents = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Test7.txt")
        Dim row() = Split(fileContents, vbNewLine)

        Dim str1 = ""
        For i As Integer = 1 To Val(row(0))
            Dim d() = Split(row(i), " ")
            Dim a(100)
            Dim b(100)
            Dim at = d(0)
            Dim bt = d(1)
            Dim c = 0
            For k As Integer = 0 To 100
                a(k) = 0
                b(k) = 0
            Next
            For j As Integer = at.Length To 1 Step -1
                a(j - 1) = Val(Mid(at, at.Length - j + 1, 1))
            Next
            For j As Integer = bt.Length To 1 Step -1
                b(j - 1) = Val(Mid(bt, bt.Length - j + 1, 1))
            Next
            For j As Integer = 0 To 100
                If a(j) + b(j) >= 10 Then
                    a(j + 1) = a(j + 1) + 1
                    c = c + 1
                End If
            Next
            str1 = str1 & c & vbNewLine
        Next

        My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\result7.txt", str1, False)
        End
    End Sub
End Class
 
 

 

Alyssa 说:
2022年12月12日 20:18

Thanks for sharing the code for operation of addition. The code is easy to follow and helps to understand the female rheumatologist near me process of addition. By aligning two integers to the right, we can add them one by one and get the correct answer.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter