求一个算法,有一个小数组和一个大数组 ,如何判断大数组中的成员是否包含所有小数组的成员?

wxl565 2011-03-31 05:45:10
小数组 a() 有三个成员 1,2,3,
大数组 b() 有10个成员 1,4,5,21,4,3,12,222,44,2

如果数组b中的数据包含了数组a的数据,则返回true .
...全文
268 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
vbman2003 2011-04-02
  • 打赏
  • 举报
回复
遍历是最简单的,也是最低效的,当然楼主示例的这么小的数组,似乎效率这层可以忽略了...
说到算法,假设如楼主所示是数据都整数的话,可以用空间换时间,思路:


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)



如上思路三个循环搞定....
booksoon 2011-04-02
  • 打赏
  • 举报
回复
逐个比较~~~
布衣散人 2011-04-01
  • 打赏
  • 举报
回复
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
用户 昵称 2011-04-01
  • 打赏
  • 举报
回复
遍历小数组,查看是否每个数都在大数组中。
无·法 2011-04-01
  • 打赏
  • 举报
回复
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
布衣散人 2011-04-01
  • 打赏
  • 举报
回复
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

贝隆 2011-04-01
  • 打赏
  • 举报
回复
遍历是最简单的
嗷嗷叫的老马 2011-04-01
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20110324/14/6759fd46-fb11-45a2-b7f3-30cdbe6219e2.html

几乎一样的需求.

很多楼都有思路或代码.
wxl565 2011-03-31
  • 打赏
  • 举报
回复
不光是要判断,还要得到 在b数组中相对应的成员的下标啊????
a=(1,2,3)
b=(1,4,5,21,4,3,12,222,44,2)

相对应的下标就是 0 9 5
oOKevinOo 2011-03-31
  • 打赏
  • 举报
回复
以下是基于Java语言:
1.先把数组B转为List对象.
2.遍历A数组,然后调用List类的contains方法来判断.
请给分哦.呵呵
clear_zero 2011-03-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sysdzw 的回复:]

VB code
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 i……
[/Quote]
这个方法挺好的,
最多instr的时候cstr(a(i))下
wxl565 2011-03-31
  • 打赏
  • 举报
回复
哦 不好意思 我没说清楚,不是字符串数组啊,是long,是下面这样的
dim a(2) as long
dim b(9) as long
a=(1,2,3)
b=(1,4,5,21,4,3,12,222,44,2)
无·法 2011-03-31
  • 打赏
  • 举报
回复
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!")

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧