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
| |
104模擬
|
M4P21
| |
104模擬
|
M4P42
| |
104正式
|
F4P11
| |
104正式
|
F4P22
| |
104正式
|
F4P31
| |
103模擬
|
M3P11
|
數學問題:判斷是否為質數
|
103模擬
|
M3P12
|
數學問題:解二元一次聯立方程式之根
|
103模擬
|
M3P21
| |
103模擬
|
M3P22
|
編碼處理:凱撒密碼
|
Function M4P12(ByVal s As String) As String
Dim x As Integer = s '將字串 轉成數字 x
'1~x 共被5整除幾次就有幾個 尾數0
Dim cnt As Integer = 0
For i = 5 To x Step 5
Dim k As Integer = i
Do While k Mod 5 = 0 ' 5的次方數會被整除多次
cnt += 1
k \= 5
Loop
Next
Return cnt
End Function
' 註: 本題若 x 太大,應另 使用遞迴解
=====輸入範例
4
6
11
25
50
20
100
6
11
25
50
20
100
=====輸出範例
0
1
2
6
12
4
24
1
2
6
12
4
24
===============
Dim x As Integer = s * 1000 - 1500 '扣掉起跳 1500公尺
Dim ans As Integer = 85 '起跳 85元
If x < 0 Then Return ans '小於1500公尺只收 85
ans += 5 * (x \ 250)
If x Mod 250 <> 0 Then ans += 5
Return ans ' 超過1500公尺的 \250每單位5元,餘數不為0再+5元
End Function
=====輸入範例
3.81
1.62
1.50
1.62
1.50
2.21
5.00
5.00
=====輸出範例
135
90
85
100
155
90
85
100
155
===============
Dim dat() = s.Split(",")
Dim a As Integer = StrReverse(dat(0)) '反轉後存為 a
Dim b As Integer = StrReverse(dat(1)) '反轉後存為 b
Dim sum As Integer = StrReverse((a + b) & "") '相加後再反轉
Return sum
End Function
=====輸入範例
1,13
123,14
213,25
172,93
100,2
30020,7
123,14
213,25
100,2
30020,7
=====輸出範例
23
263
463
13
3
102
263
463
13
3
102
===============
Function F4P11(ByVal s1 As String, ByVal s2 As String) As String
Dim x As Integer = s1 '共停過x個樓層
Dim dat() = s2.Split(",") '有哪幾層以,號隔開:split切好分成dat(0)~dat(x-1)
Dim ans As Integer = 0
For i = 1 To x - 1
Dim f As Integer = dat(i)
If f > dat(i - 1) Then
ans += 20 * (f - dat(i - 1))
Else
ans += 10 * (dat(i - 1) - f)
End If
Next
Return ans
End Function
=====輸入範例 {這題一次兩列,s1,s2}
3
3,8,5
7
2,9,7,3,4,6,1
3,8,5
7
2,9,7,3,4,6,1
3
18,19,20
3
2,5,2
18,19,20
3
2,5,2
=====輸出範例
130
310
40
90
310
40
90
===============
Function F4P22(ByVal s As String) As String
' F4P22_最大公約數計算
Dim dat() = s.Split(",") ' 以,號隔開:split切好分成dat(0)~dat(m-1)
Dim m As Integer = dat.Length
Dim ans As Integer = dat(0)
For i = 1 To m - 1
ans = gcd(ans, dat(i))
Next
Return ans
End Function
Function gcd(ByVal a As Integer, ByVal b As Integer) As Integer
'求兩個非0正整數a,b的最大公約數, 運算後 更新後的 b即是
Dim r As Integer = a Mod b
Do Until r = 0
a = b
b = r
r = a Mod b
Loop
Return b
End Function
=====輸入範例
20,8,30
10,20,30
8,12
24,60,36
2,65535
2,3,4,5,6
10,20,30
8,12
24,60,36
2,65535
2,3,4,5,6
=====輸出範例
2
10
4
12
1
1
10
4
12
1
1
===============
Function F4P31(ByVal s As String) As String
Dim x As Integer = s
Dim cnt As Integer = 0 '位元1有幾個
Do Until x = 0
cnt += x Mod 2
x \= 2
Loop
Return cnt ' 除2的餘數為1 加入 cnt
End Function
=====輸入範例
1025
65535
0
3
65535
0
3
=====輸出範例
2
16
0
2
16
0
2
===============
0 意見:
張貼留言