Problem7:進位判斷(13%)
加法的運算是把 2 個整數靠右對齊,然後由右至左,一位一位相加。如果相加的結果大
於等於 10 就有進位(carry)發生。請寫個程式來判斷兩個正整數相加時,產生了幾次進位的
情況。
輸入說明:
第一行的數字,表示有幾組測試資料,第二行開始的每一行即為一筆測試資料。每一行
輸入的資料有兩個正整數,以一個空格分開,每個整數的長度均小於 100 位數。
輸出說明:
對每一筆測試資料,輸出相加後有幾次進位的次數。
輸入範例:
2
123 456
555 555
輸出範例:
0
3
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
Problem5:字串處理(10%)
Problem5:字串處理(10%)
給一個字串,請寫一個程式,計算此字串中,英文字元有幾個?輸入說明:輸入檔第一行表示有幾組測試資料,第二行開始的每一行即為一筆測試資料,每行最多有 1000 個字元。輸出說明:對每一筆測試資料,輸出字串中英文字元的個數。輸入範例:2abc123def456133adfag3428a2fwqgq2輸出範例:611
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 & "\Test5.txt") Dim row() = Split(fileContents, vbNewLine) Dim str1 = "" For i As Integer = 1 To Val(row(0)) If row(i) = "" Then Exit For Dim c = 0 Dim a = row(i) For j As Integer = 1 To a.Length If Asc(Mid(a, j, 1)) >= 97 And Asc(Mid(a, j, 1)) <= 122 Then c = c 1 ElseIf Asc(Mid(a, j, 1)) >= 65 And Asc(Mid(a, j, 1)) <= 90 Then c = c 1 End If Next str1 = str1 & c & vbNewLine Next My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\result5.txt", str1, False) End End Sub End Class
題目沒說明字串裡的英文字大小寫,為了以防萬一,就多了一個判斷 大寫也算進去
話說半夜練習程式安靜許多
我都不想睡了
我都不想睡了
Problem6:判斷是否為 11 的倍數(14%)
給一個正整數 n,請寫一個程式,判斷 n 是否為 11 的倍數?
輸入說明:第一行的數字,表示有幾組測試資料,第二行開始即為第一筆測試資料。每筆測試資料為一個正整數,數字的位數,最高有可能到 1000 位。輸出說明:對每一筆測試資料,輸出是否為 11 的倍數。是的話請輸出 1,反之則輸出 0。輸入範例:224841983960121輸出範例:01
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 & "\Test6.txt") Dim row() = Split(fileContents, vbNewLine) Dim result = "" For j As Integer = 1 To Val(row(0)) Dim str = row(j) Dim a = 0 For i As Integer = 1 To str.Length If (i 1) Mod 2 = 0 Then a = a Val(Mid(str, i, 1)) Else a = a - Val(Mid(str, i, 1)) End If Next Dim str1 = "" If a = 0 Or a = 11 Then str1 = "1" '是11的倍數 Else str1 = "0" End If result = result & str1 & vbNewLine Next My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\result6.txt", result, False) End End Sub End Class
這題考慮到的問題是 最高有可能到 1000 位
是個天大的數字阿
所以並不能方式直接判斷 數字 MOD 11 = 0
當然
要是這樣出題還需要考嗎
解決方法是
a = 奇數位數的數字相加
b = 偶數位數的數字相加
假若a - b = 0 或者 11 就是11的倍數
Problem2:數字分解(11%)
Problem2:數字分解(11%)
給一個偶數 n,請你將 n 分解成兩個質數的和,也就是說,這兩個質數相加的和必須等於 n。輸入說明:第一行的數字,表示有幾組測試資料,第二行開始即為第一筆測試資料,每筆測試資料為一個數字,數字的範圍為[4, 10000]間的偶數。輸出說明:對輸入的每筆測試資料,分別輸出 2 個質數,用一個空白做為區隔,請由小到大排列。輸入範例:212100輸出範例:5 73 97
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 & "\Test2.txt") Dim row() = Split(fileContents, vbNewLine) Dim str1 = "" For i As Integer = 0 To Val(row(0)) If row(i) = "" Then Exit For Dim k = Val(row(i)) For j As Integer = k - 1 To 1 Step -1 If f(k - j) = True And f(k - (k - j)) = True Then '條件:兩個都是質數 str1 = str1 & k - j & " " & k - (k - j) & vbNewLine Exit For End If Next Next My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\result2.txt", str1, False) End End Sub Function f(ByVal n) '質數判斷 Dim c = 0 Dim a = False For i As Integer = 1 To n If n Mod i = 0 Then c = c 1 Next If c = 2 Then a = True Return a End Function End Class
Problem1:數字刪除(12%)
Problem1:數字刪除(12%)
給 n 個數字,請你在這 n 個數字中,找出所有重覆出現的數字,並把它刪除。最後計算刪除後剩餘的數字個數。輸入說明:第一行的數字,表示有幾組測試資料,第二行開始即為第一筆測試資料,每行不會超過100 個數字,每個數字之間用一個空白做為區隔,數字的範圍為[0, 10000]間的整數。輸出說明:對輸入的每筆測試資料,分別輸出刪除完後剩下的數字個數。輸入範例:21 2 3 4 5 6 7 8 9 2 12 4 6 8 10輸出範例:75
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 & "\Test1.txt") Dim row() = Split(fileContents, vbNewLine) Dim str1 = "" For i As Integer = 1 To Val(row(0)) Dim aList = New ArrayList If row(i) = "" Then Exit For Dim d() = Split(row(i), " ") For j As Integer = 0 To d.Length - 1 If alist.Contains(d(j)) = False Then '若不存在則加入 alist.Add(d(j)) Else '否則從aList中刪除 aList.Remove(d(j)) End If Next str1 = str1 & alist.Count & vbNewLine Next My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\result1.txt", str1, False) End End Sub End Class