105正-F5P11 計算字數 ✔(上一篇已傳)
105正-F5P12 摩斯電碼
105正-F4P21 網段ID
105模-M5P11 質數 ✔
105模-M5P22 行、字數、字元數 ✔
105模-M5P41 尋找對稱字串
105模-M5P42 十進制轉二進制 ✔
105模-M5P21 前綴字串and後綴字串
補充大數 N! (0<N<1000) ✔(上一篇已傳)
內建函式整理: ======================
1.Math.Sqrt(x):內建數學類函式,算出數值 x 的正平方根後傳回
2.EOF(fn):檔案類函式,判斷是否已讀至檔案尾端,是則傳回 True
3.StrReverse(s):將字串 s 的內容反轉後傳回
例 s="120" 則傳回 "021"
======================================
105模擬 P11:質數 ================================
Function P11(ByVal x As Integer) As String
'傳入一個整數 x ,判斷是否為質數,是傳回字串T,否傳回F
p11 = "T"
If x < 2 Then Return "F"
For k = 2 To Math.Sqrt(x)
If x Mod k = 0 Then Return "F"
Next
End Function
105模擬 P22:行、字數、字元數 ======================
Function P22(ByVal fn As Integer) As String
'傳入一個整數 fn 為要讀入的 檔案編號
Dim a, b, c As Integer
Do Until EOF(fn)
Dim line = LineInput(fn)
a += 1
c += line.Length
Dim dat() = line.Split(" ")
b += dat.Length
Loop
Return a & "," & b & "," & c
End Function
105模擬 P42:十進制轉二進制 ========================
Function P42(ByVal s As String) As String
Dim dat() = s.Split(",")
Dim a As Integer = StrReverse(dat(0))
Dim b As Integer = StrReverse(dat(1))
Dim sum As Integer = StrReverse((a + b) & "")
P42 = d2b(sum) ' 呼叫自訂函式d2b將十進位轉二進位
End Function
Function d2b(ByVal sum As Integer) As String
'將傳入的十進位數值 sum 傳為二進位後傳回 字串
d2b = ""
If sum = 0 Then Return "0"
Do Until sum = 0
d2b = sum Mod 2 & d2b
sum \= 2
Loop
End Function
Dim dat() = s.Split(",")
Dim a As Integer = StrReverse(dat(0))
Dim b As Integer = StrReverse(dat(1))
Dim sum As Integer = StrReverse((a + b) & "")
P42 = d2b(sum) ' 呼叫自訂函式d2b將十進位轉二進位
End Function
Function d2b(ByVal sum As Integer) As String
'將傳入的十進位數值 sum 傳為二進位後傳回 字串
d2b = ""
If sum = 0 Then Return "0"
Do Until sum = 0
d2b = sum Mod 2 & d2b
sum \= 2
Loop
End Function
0 意見:
張貼留言