參考解
Function M4P12(ByVal s As String) As String
' M4P12_N!尾數的0
Dim x As Integer = s
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 '1~x 共被5整除幾次就有幾個 尾數0
End Function
Function M4P21(ByVal s As String) As String
' M4P21_計程車費率計算
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
Function M4P22(ByVal s As String) As String
' M4P22_迴文
Dim n As Integer = s.Length '字串長度
Dim m As Integer = n \ 2 '字串長度的一半
'由第1字與最後1字比,第2字與倒數第2字比, … 第m字與倒數第m字比
For i = 1 To m
If Mid(s, i, 1) <> Mid(s, n - i + 1, 1) Then Return "N"
Next
Return "Y" ' 皆沒有不同,即是迴文
End Function
Private Sub Button4_Click( . . .) Handles Button4.Click
' F4P11_電梯電費計算系統 ,輸入有兩列,以兩個文字方塊讀入s1,s2
Dim s1 As String = TextBox1.Text '例:7
Dim s2 As String = TextBox2.Text '例:2,9,7,3,4,6,1
Label1.Text = F4P11(s1, s2)
End Sub
Function F4P11(ByVal s1 As String, ByVal s2 As String) As String
' F4P11_電梯電費計算系統
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
Function F4P22(ByVal s As String) As String
' F4P22_最大公約數計算
Dim dat() = s.Split(",") ' '共有以,號隔開不一定幾個[假設m個]的數字: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
Function F4P31(ByVal s As String) As String
' F4P31_計算位元為1的個數
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
Function M5P42(ByVal s As String) As String
' M5P42_十進制數轉二進制數
Dim dat() = s.Split(",")
dat(0) = StrReverse(dat(0)) ' 或 Strings.StrReverse 將字串內容反轉
dat(1) = StrReverse(dat(1))
Dim x As Integer = dat(0), y As Integer = dat(1)
Dim sum As Integer = StrReverse(x + y)
Dim ans As String = ""
' 以下將 sum轉為 二進制,每除2一次的餘數串在前面
Do While sum <> 0
ans = sum Mod 2 & ans
sum \= 2
Loop
Return ans
End Function
Function M4P12(ByVal s As String) As String
' M4P12_N!尾數的0
Dim x As Integer = s
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 '1~x 共被5整除幾次就有幾個 尾數0
End Function
Function M4P21(ByVal s As String) As String
' M4P21_計程車費率計算
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
Function M4P22(ByVal s As String) As String
' M4P22_迴文
Dim n As Integer = s.Length '字串長度
Dim m As Integer = n \ 2 '字串長度的一半
'由第1字與最後1字比,第2字與倒數第2字比, … 第m字與倒數第m字比
For i = 1 To m
If Mid(s, i, 1) <> Mid(s, n - i + 1, 1) Then Return "N"
Next
Return "Y" ' 皆沒有不同,即是迴文
End Function
Private Sub Button4_Click( . . .) Handles Button4.Click
' F4P11_電梯電費計算系統 ,輸入有兩列,以兩個文字方塊讀入s1,s2
Dim s1 As String = TextBox1.Text '例:7
Dim s2 As String = TextBox2.Text '例:2,9,7,3,4,6,1
Label1.Text = F4P11(s1, s2)
End Sub
Function F4P11(ByVal s1 As String, ByVal s2 As String) As String
' F4P11_電梯電費計算系統
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
Function F4P22(ByVal s As String) As String
' F4P22_最大公約數計算
Dim dat() = s.Split(",") ' '共有以,號隔開不一定幾個[假設m個]的數字: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
Function F4P31(ByVal s As String) As String
' F4P31_計算位元為1的個數
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
Function M5P42(ByVal s As String) As String
' M5P42_十進制數轉二進制數
Dim dat() = s.Split(",")
dat(0) = StrReverse(dat(0)) ' 或 Strings.StrReverse 將字串內容反轉
dat(1) = StrReverse(dat(1))
Dim x As Integer = dat(0), y As Integer = dat(1)
Dim sum As Integer = StrReverse(x + y)
Dim ans As String = ""
' 以下將 sum轉為 二進制,每除2一次的餘數串在前面
Do While sum <> 0
ans = sum Mod 2 & ans
sum \= 2
Loop
Return ans
End Function