105正-F5P11 計算字數 ✔
105正-F5P12 摩斯電碼
105正-F4P21 網段ID
105模-M5P11 質數
105模-M5P22 行、字數、字元數
105模-M5P41 尋找對稱字串
105模-M5P42 十進制轉二進制
105模-M5P21 前綴字串and後綴字串
補充大數 N! (0<N<1000) ✔
內建函式整理: ======================
1.s.split(ds) '以 ds 為分隔字,將字串s分成多個資料,傳回陣列
例: s="1,23,4" : dim dat() = s.split(",")
會傳回dat為三個元素:dat(0)="1"、dat(1)="23"、dat(2)="4"
2.UBound(dat):取得陣列dat的最大註標值傳回數值
以上例:會傳回 2,就是dat的最大註標元素為dat(2)
3.Trim(s):將字串 s 的最左及最右空白刪除傳回字串
4.Instr(s1,s2):在字串s1中找看是否有s2,若沒有傳回 0 ,有則傳回位置
5.s.replace(s1,s2):在字串s中找第1個s1有找到換成s2,傳回換好的字串
======================================
105正式P22:最大公約數計算================================
Function P22(ByVal s As String) As Integer
Dim dat() = s.Split(",") '以空白分割
Dim g As Integer
P22 = 1
For j = 0 To UBound(dat) - 1 ' 兩兩求gcd,所有中 找最大
For k = j + 1 To UBound(dat)
g = gcd(dat(j), dat(k))
If g > P22 Then P22 = g
Next
Next j
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
105正式P11:計算字數================================
Function P11(ByVal s As String) As Integer
P11 = 0
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
補充大數 N! (0<N<1000)=========================
Public Class Form1
'
建一個標籤 Label1,一個按鈕 Button1
Const MaxN As Integer = 2600 ' 1000! 共2568 位數
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim a(MaxN) As Integer
Dim an As Integer = 1 ' 1!=1 ,一位數
a(0) = 1
Dim n As Integer = InputBox("輸入n 1~1000")
For k = 2 To n
bmuli(a, an, k)
Next
Label1.Text = printn(a, an) & vbNewLine
End Sub
Sub bmuli(ByVal a() As Integer, ByRef an As Integer, ByVal k As Integer)
' 大數 a 乘 整數 k
For i = 0 To an - 1
a(i) *= k
Next
an += 3
For i = 0 To an - 1
If a(i) >= 10 Then
a(i + 1) += (a(i) \ 10)
a(i) = a(i) Mod 10
End If
Next
Do While an >= 0 AndAlso a(an) = 0
an -= 1
Loop
an += 1
If an = 0 Then an = 1
End Sub
Function printn(ByVal a() As Integer, ByVal an As Integer) As String
' 將 a 陣列倒轉列出 => 字串 ans 轉回 ans
Dim ans = ""
For i = an - 1 To 0 Step -1
ans &= a(i)
Next
Return ans
End Function
End Class
' 範例輸入
10
20
50
88
==============
'範範例輸出
3628800
2432902008176640000
30414093201713378043612608166064768844377641568960512000000000000
185482642257398439114796845645546284380220968949399346684421580986889562184028199319100141244804501828416633516851200000000000000000000
0 意見:
張貼留言