循序搜尋法:先輸入的n個不一定排序好的資料,例如n=5,而資料是2,4,3,6,9,再輸入要找的數,如果要找的數是4則印出2代表發現的位置。若是5則印出”Not found”。
順便註解一下....謝謝
2007-01-18 16:44:06 · 2 個解答 · 發問者 sonya 1 in 電腦與網際網路 ➔ 程式設計
不好意思...想請問一下WJS:
有些名詞我不懂耶!!
ex: Dim A$, S$, N% -> 是要做啥的呀??
InStr=? UBound=? Split=?
可以幫我解釋一下嗎? 謝謝!!
2007-01-19 15:24:12 · update #1
Private Sub Command1_Click()
Dim A$, S$, N%
S = ",2,4,3,6,9,605,65,111,101,22,5,99,11,A,AA,DSD,B,"
'輸入欲找尋之值
A = InputBox("輸入欲找尋之值")
'以InStr找S裡面有沒有 "," & A & ","
N = InStr(S, "," & A & ",")
If N Then '有找到的話
'以N為準切割S後用Split分割成陣列,此時陣列數量即為答案
Print UBound(Split(Left(S, N), ","))
Else '沒找到就輸出Not Find
Print "Not Find"
End If
End Sub
2007-01-20 04:55:53 補充:
Dim A$, S$, N% → Dim A As String , S As String , N As Inteser
$→String
%→ Integer
&→Long
!→Single
#→Double
2007-01-18 20:49:35 · answer #1 · answered by W.J.S. 7 · 0⤊ 0⤋
'=============================================
' 此法可用於資料為任何值數(有分Integer,Long....)
' 若只限 0~9,可用InStr函數,請自行練習,在此不贅述
'
' 以下的註解是簡單略過,若是作業還是要自己多實作
' 技術問題再請教他人,謝謝,加油囉!
'=============================================
Private Sub Command1_Click()
Dim n As Integer, i As Integer
Dim sData As String
Dim sString As String
Dim rd() As String
sData = "2,4,3,6,9,5,1,7,8" '不一定排序好的資料
rd = Split(sData, Chr(44)) '將資料存放在陣列中
n = Val(InputBox("請先輸入資料筆數( 1 ~ 9 ):", "循序搜尋法"))
sString = InputBox("請輸入要找的數( 1 ~ 9 ):", "循序搜尋法")
i = 1
Do Until rd(i - 1) = sString '比對輸入值資料
i = i + 1
If i > n Then '比對次數 > 輸入 n 值代表找不到
Debug.Print "Not found"
Exit Sub
End If
Loop
Debug.Print i
End Sub
2007-01-18 19:24:03 · answer #2 · answered by Abel 2 · 0⤊ 0⤋