关于vb在数组中的顺序查找,高手帮忙!!!
vb\u7f16\u4e00\u4e2a\u5b9e\u73b0\u987a\u5e8f\u67e5\u627e\u7684\u51fd\u6570\u8fc7\u7a0b.\u8c03\u7528\u6b64\u8fc7\u7a0b\u5728\u4e00\u4e2a\u670915\u4e2a\u5143\u7d20\u7684\u6570\u7ec4\u4e2dPrivate Sub Form_Load()
Dim B(15) As String
B(3) = "A"
B(9) = "B"
B(11) = "C"
MsgBox SearchValInArray(B, "B")
End Sub
Private Function SearchValInArray(ByRef a() As String, ByVal Val As String) As Integer
Dim R As Long
SearchValInArray = -1
For R = 0 To UBound(a)
If a(R) = Val Then
SearchValInArray = R
Exit For
End If
Next
End Function
\u5927\u6982\u5c31\u662f\u8fd9\u4e48\u4e2a\u610f\u601d
Option Base 1
Private Function find(a() As Single, x As Single) As Integer
Dim n%, p%
n = UBound(a) '\u6570\u7ec4\u5143\u7d20\u4e2a\u6570
For p = 1 To n '\u5faa\u73af\u6bcf\u4e2a\u5143\u7d20
If x = a(p) Then Exit For '\u5982\u679c\u627e\u5230\u76f8\u540c \u5219\u9000\u51fa\u5faa\u73af \u6b64\u65f6\u7684P\u503c\u65e2\u662f\u7ed3\u679c
Next p
'\u5982\u679c\u6ca1\u627e\u5230 P\u503c\u5c06\u4f1a\u662f N+1
If p > n Then p = 0
find = p
End Function
Private Sub Form_click()
Dim test(10) As Single
Dim x As Single
Randomize
For i = 1 To 10
test(i) = Int(Rnd * 10 + 1)
Next
x = 2 '
MsgBox find(test, x)
End Sub
if a(i)=x then
msgbox x & "是数组a中的第" & i & "个数"//如果找到的
exit for//关键是这里,如果找到的话就退出循环了,这时I不再增加,但如果一直找不到的话,I就不断循环+1,到最后一次i会变成n+1,这时候超过循环上限退出循环,所以如果i=n+1就代表找不到数据在数组里了.
end if
next
补充:
最大值是n,但当最后一次的时候,FOR循环还会继续加1的,然后判断到比n大,就退出循环了,所以这时候i = n+1
比如:for i = 1 to 3 ... next i, 当退出循环后i = 4
i是循环一次自加1
当循环中遇到符合条件的ExitFor后,i还执行加1
所以判定条件为 if i=n+1才能是 判定所有数组都遍历完了
for i=1 to 10
next i
这段循环执行以后,i=11。相当于i加到11以后,VB系统发现已不满足循环条件,就退出了循环。你用F8单步跟踪一下程序就能看清楚了。
for中如果没找到i应该是n+1啊,这有什么问题吗?
是到N啊,但要跳出循环不就是n+1了吗
绛旓細For i = 1 To num '鍦ㄥ垪琛2涓樉绀鎺掑簭鍚庣殑鏁版嵁 List2.AddItem Str(i) + " " + Str(d(i))Next i End Sub Private Sub Command3_Click()Key = Val(Text2.Text)j = 1 Do While j <= num If d(j) = Key Then Label5.Caption = "鍦ㄦ暟缁勭殑 " + Str(j) + " 浣嶇疆涓...
绛旓細鈶F({0,1},$A$2:$A$7,$B$2:$B$7)鏄湪鍐呭瓨涓婇潰灏$A$2:$A$7锛$B$2:$B$7涓ゅ垪杩涜浜嗗璋冿紝鍙互閫氳繃鐐瑰嚮鑿滃崟鏍忎笂闈㈢殑銆愬叕寮忋戯紝銆愬叕寮忔眰鍊笺戯紝銆愭眰鍊笺戣繘琛屾煡鐪嬶紝涔嬪悗灏嗕細鐪嬪埌寤虹珛鐨勭粍鏁閲岄潰椤哄簭宸茬粡瀵硅皟銆 鈶⑩2鈥濇槸瑕佹煡鎵炬樉绀哄嚭鐨勭粨鏋滀篃灏鍦ㄦ煡鎵鍖哄煙閲岄潰绗簩鍒椼 鈶b0鈥濅负绮剧‘鏌ユ壘...
绛旓細] args) { int[] res = {1,2,5,6,7,8,9,3,10,11,12,14,15,22,21,17,86,99,55,24,23,25,28,31,65};int length = res.length;int j = 25;for (int i = 0; i < length; i++) { if(res[i] == j) { System.out.println("浣嶄簬鏁扮粍锛" + i);} } } ...
绛旓細绛旀鏄疉銆傚簲鐢椤哄簭鏌ユ壘娉曟椂锛屾煡鎵1闇瑕佹瘮杈1娆★紱搴旂敤浜屽垎鏌ユ壘娉曟椂锛屾煡鎵1闇瑕佹瘮杈3娆★紝鎬绘鏁颁负4娆°傚叾浠栧厓绱犵殑鎬绘煡鎵炬鏁板潎瓒呰繃4娆°
绛旓細Randomize For i = 1 To 10 a(i) = Int(100 * Rnd)Print a(i);Next i For i = 1 To 9 For j = 1 To 10 - i If a(j) > a(j + 1) Then t = a(j): a(j) = a(j + 1): a(j + 1) = t End If Next j Next i Print: Print Print "鎺掑簭浠ュ悗鐨勬暟鏄細"For...
绛旓細== x){break;}}if(i < n){return 1;}else {return 0;}}void main(){int arr[] = {49, 38, 65, 97, 76, 13, 27};int n = 7;int x = 76;if(find(arr, n, x) == 1){printf("%d鍦ㄦ暟缁勪腑瀛樺湪\n", x);}else{printf("%d鍦ㄦ暟缁勪腑涓嶅瓨鍦╘n", x);}}杩愯娴嬭瘯锛...
绛旓細銆愮瓟妗堛戯細D 鏈鑰冩煡甯歌鏌ユ壘绠楁硶鏃堕棿澶嶆潅搴︺椤哄簭琛ㄦ煡鎵撅細鏈濂 O(1) 鏈鍧 O(n) 鏈缁 O(n)鎶樺崐鏌ユ壘锛氭渶缁坙ogn浜屽弶鎺掑簭鏍戯細鏈缁坙ogn骞宠 浜屽弶鏍戯細logn鍝堝笇琛ㄦ硶锛堟暎鍒楄〃锛夛細O(1)锛屼絾鏄瀯寤哄搱甯岃〃闇瑕丱(n)鍒嗗潡鏌ユ壘锛歄(logn)
绛旓細\n");system("PAUSE");return 0;}椤哄簭鏌ユ壘鏄寜鐓у簭鍒楀師鏈夐『搴忓鏁扮粍杩涜閬嶅巻姣旇緝鏌ヨ鐨勫熀鏈煡鎵剧畻娉曘傚浜庝换鎰忎竴涓簭鍒椾互鍙婁竴涓粰瀹氱殑鍏冪礌锛屽皢缁欏畾鍏冪礌涓庡簭鍒椾腑鍏冪礌渚濇姣旇緝锛岀洿鍒版壘鍑轰笌缁欏畾鍏抽敭瀛楃浉鍚岀殑鍏冪礌锛屾垨鑰呭皢搴忓垪涓殑鍏冪礌涓庡叾閮芥瘮杈冨畬涓烘銆傚嚱鏁板疄鐜板涓嬶細int sq_search(keytype keyp[],int n...
绛旓細浠g爜濡備笅锛歴earch(int a[],int x){ int i;for(i=0;i
绛旓細(4)鎻掑叆娉曪細姣忚緭鍏ユ垨鐢熸垚涓涓暟椹笂鎻掑叆鍒版暟缁勪腑浣垮叾鏈夊簭銆5.鏁板垪鐨勬彃鍏ャ佸垹闄ゅ拰閲嶇粍 (1)鎻掑叆锛氬鍘熸暟缁勮繘琛屾墿鍏咃紝寰幆鏌ユ壘鎻掑叆浣嶇疆(閫愪釜姣旇緝)锛屾壘鍒板悗锛屼粠鍚庡悜鍓渚濇绉诲姩姣忎竴涓暟瀛楋紝鐩村埌璇ヤ綅缃紝鐒跺悗灏嗘暟鎹彃鍏ャ(2)鍒犻櫎锛氫笌鎻掑叆绫讳技锛屼篃鏄厛鏌ユ壘浣嶇疆锛屾壘鍒板悗锛屽皢璇ヤ綅缃互鍚庣殑姣忎竴涓厓绱犱緷娆″墠绉汇