7,763
社区成员
发帖
与我相关
我的任务
分享
dim a,b
dim i as long
dim Idx() as long
dim maxIdx as long
dim tmp()
a = Array(1, 2, 3)
b = Array(1, 4, 5, 21, 4, 3, 12, 222, 44, 2)
'先循环小数组得到一个最大值
for i=1 to ubound(a)
if a(i)>a(maxIdx) then maxIdx=i
next
'按最大值声明一个存取大数组下标的数组
redim Idx(a(maxIdx))
'循环大数数组写入对应值的下标
for i=0 to ubound(b)
if b(i)<= a(maxIdx) then
if Idx(b(i))=0 then Idx(b(i))=i+1 '如果有重复值,只取第一次出现的下标
end if
next
'循环小数组,验证
tmp=a
for i=0 to ubound(a)
if idx(a(i))>0 then
tmp(i)=idx(a(i))-1
else
msgbox "b 不是包含 a"
exit sub
end if
next
msgbox "b 在 a中对应的下标为: "& join(tmp)
Private Sub Command1_Click()
Dim c As Variant
Dim d As Variant
Dim a(2) As Long
Dim b(9) As Long
Dim i As Integer
Dim j As Integer
Dim blnHave As Boolean
Dim blnFindAll As Boolean
c = Array(1, 2, 3)
d = Array(1, 4, 5, 21, 4, 3, 12, 222, 44, 2)
For i = 0 To UBound(c)
a(i) = c(i)
Next i
For i = 0 To UBound(d)
b(i) = d(i)
Next i
If UBound(a) > 0 And UBound(b) > 0 Then
blnFindAll = True
Else
blnFindAll = False
End If
For i = 0 To UBound(a)
blnHave = False
For j = 0 To UBound(b)
If a(i) = b(j) Then
Me.Print a(i) & " 存在, 位置 " & j
blnHave = True
Exit For
End If
Next j
If blnHave = False Then
blnFindAll = False
End If
Next i
Me.Print IIf(lbnfindall, "", "没有") & "全部找到"
End Sub
Private Sub Form_Load()
Dim a, b
a = Array(1, 2, 3)
b = Array(1, 4, 5, 21, 4, 3, 12, 222, 44, 2)
Dim i&, j&, isExist As Boolean, isFind As Boolean
isExist = True
For i = 0 To UBound(a)
isFind = False
For j = 0 To UBound(b)
If a(i) = b(j) Then
Debug.Print j
isFind = True
Exit For
End If
Next
If Not isFind Then
isExist = False
Exit For
End If
Next
MsgBox IIf(isExist, "b包含a!", "b不包含a!")
End Sub
Private Sub Command1_Click()
Dim c As Variant
Dim d As Variant
Dim a(2) As Long
Dim b(9) As Long
Dim i As Integer
Dim j As Integer
Dim blnHave As Boolean
c = Array(1, 2, 3)
d = Array(1, 4, 5, 21, 4, 3, 12, 222, 44, 2)
For i = 0 To UBound(c)
a(i) = c(i)
Next i
For i = 0 To UBound(d)
b(i) = d(i)
Next i
For i = 0 To UBound(a)
blnHave = False
For j = 0 To UBound(b)
If a(i) = b(j) Then
Me.Print a(i) & " 存在, 位置 " & j
blnHave = True
Exit For
End If
Next j
Next i
End Sub
dim i%,s$,isOK as boolean
isOK=true
s="," & join(b,",") & ","
for i=0 to ubound(a)
if instr(s,"," & a(i) & ",")=0 then
isOK =false
exit for
end if
next
msgbox iif(isOK,"b包含a!","b不包含a!")