4/10(一)測驗題單 (題目名加上連結的表示範例完成)
假設由文字方塊輸入,呼叫 Function MyPxx( S As String ) As String後傳回要輸出的字串
以下只有M5P11附主程式部份,其餘類似
以下只有M5P11附主程式部份,其餘類似
105模擬
|
M5P11
| |
105模擬
|
M5P22
| |
105模擬
|
M5P42
| |
105正式
|
F5P11
| |
105正式
|
F5P12
| |
105正式
|
F5P21
| |
105正式
|
F5P22
| |
104模擬
|
M4P12
|
數學問題:N!尾數的0
|
104模擬
|
M4P21
|
其他:計程車費率計算
|
104模擬
|
M4P42
|
其他:數字反轉後相加
|
104正式
|
F4P11
|
生活問題:電梯電費計算系統
|
104正式
|
F4P22
|
數學問題:最大公約數計算
|
104正式
|
F4P31
|
其他:計算位元為1的個數
|
103模擬
|
M3P11
|
數學問題:判斷是否為質數
|
103模擬
|
M3P12
|
數學問題:解二元一次聯立方程式之根
|
103模擬
|
M3P21
| |
103模擬
|
M3P22
|
編碼處理:凱撒密碼
|
====== 105正式 F5P11 字串問題:計算字數
Function F5P11(ByVal s As String) As String
ttrim(s, " ", " ") '呼叫 ttrim自訂副程式,將多個空格改為1個
Dim dat() = s.Split(" ")
Return dat.Length
End Function
Sub ttrim(ByRef s As String, ByVal a As String, ByVal b As String)
s = Trim(s) ' trim是內建函式,去首尾的空白
Do While (InStr(s, " ") > 0) '有兩個空白傳回值>0
s = s.Replace(" ", " ")
Loop
End Sub
=====輸入範例
This is a sample file.
Hello World!!
Hi!
Bigtable timestamps are integers.
Hello World!!
Hi!
Bigtable timestamps are integers.
=====輸出範例
5
2
1
4
2
1
4
===============
====== ' 105正式 P12:摩斯電碼
' 本題 摩斯字串 對照 0~4 、 5~9
' 以 Array.IndexOf( )函數查找即可
Dim ms() As String = {"-----", ".----", "..---", "...--", "....-",
".....", "-....", "--...", "---..", "----."}
Function F5P12(ByVal s As String) As String
F5P12 = ""
ttrim(s, " ", " ") '去首尾空白及中間2個以上空白換成1個
Dim dat() = s.Split(" ")
For Each m In dat
F5P12 &= Array.IndexOf(ms, m)
Next
End Function
Sub ttrim(ByRef s As String, ByVal a As String, ByVal b As String)
s = Trim(s)
Do While (InStr(s, " ") > 0)
s = s.Replace(" ", " ")
Loop
End Sub
=====輸入範例
.---- ..--- ...--
....- ..... -....
--... ---.. ----.
....- ..... -....
--... ---.. ----.
.---- .---- -----
----- .---- ..--- ...-- ....- ..... -.... --... ---.. ----.
----- .---- ..--- ...-- ....- ..... -.... --... ---.. ----.
=====輸出範例
123
456
789
110
0123456789
456
789
110
0123456789
====== ' 105正式 P21:網段ID
Function F5P21(ByVal s As String) As String
Dim a, b As Integer
Dim p As Integer = InStr(s, "/")
Dim sa() = Mid(s, 1, p - 1).Split(".")
Dim sb() = Mid(s, p + 1).Split(".")
P21 = ""
For j = 0 To 3 ' IP分4段,以.隔開
a = sa(j)
b = sb(j)
If j > 0 Then P21 &= "."
P21 &= (a And b) '直接以 And 做 bit運算
Next
Dim p As Integer = InStr(s, "/")
Dim sa() = Mid(s, 1, p - 1).Split(".")
Dim sb() = Mid(s, p + 1).Split(".")
P21 = ""
For j = 0 To 3 ' IP分4段,以.隔開
a = sa(j)
b = sb(j)
If j > 0 Then P21 &= "."
P21 &= (a And b) '直接以 And 做 bit運算
Next
End Function
=====輸入範例
139.175.153.252/255.255.0.0
192.168.10.65/255.255.255.224
10.240.168.19/255.255.192.0
192.168.10.65/255.255.255.224
10.240.168.19/255.255.192.0
172.16.115.65/255.255.254.0
192.168.168.19/255.255.255.240
192.168.168.19/255.255.255.240
=====輸出範例
139.175.0.0
192.168.10.64
10.240.128.0
172.16.114.0
192.168.168.16
192.168.10.64
10.240.128.0
172.16.114.0
192.168.168.16
===============
====== ' 105正式 P22:最大公約數計算
Function F5P22(ByVal s As String) As String
Dim dat() = s.Split(",") '以空白分割
Dim g As Integer
Dim Ans = 1
For j = 0 To UBound(dat) - 1 ' 兩兩求gcd,所有中 找最大
For k = j + 1 To UBound(dat)
g = gcd(dat(j), dat(k))
If g > Ans Then Ans = g
Next
Next j
Return Ans
End Function
Function gcd(ByVal a As Integer, ByVal b As Integer) As Integer
Dim r As Integer = a Mod b
Do Until r = 0
a = b
b = r
r = a Mod b
Loop
Return b
End Function
Dim g As Integer
Dim Ans = 1
For j = 0 To UBound(dat) - 1 ' 兩兩求gcd,所有中 找最大
For k = j + 1 To UBound(dat)
g = gcd(dat(j), dat(k))
If g > Ans Then Ans = g
Next
Next j
Return Ans
End Function
Function gcd(ByVal a As Integer, ByVal b As Integer) As Integer
Dim r As Integer = a Mod b
Do Until r = 0
a = b
b = r
r = a Mod b
Loop
Return b
End Function
=====輸入範例
10,20,30,40
125,15,25
8,12
125,15,25
8,12
24,60,36
2,65535
2,3,4,5,6
2,65535
2,3,4,5,6
=====輸出範例
20
25
4
12
1
3
25
4
12
1
3
===============
0 意見:
張貼留言