求该数组对比的简化代码,感谢各位热心人!

shier2817 2009-05-15 11:38:22
加精
假设:

Dim a() As Byte, b() As Byte
ReDim a(2)
a(0) = 2: a(1) = 1: a(2) = 1
……这里通过某些操作使得 b 也拥有了3个有效元素,但内容随机


我要实现的效果是:

如果 b 为:2,1,1 则什么都不做
如果 b 为:x,2,1 则 ReDim b 为 x,2,1,1
如果 b 为:x,y,2 则 ReDim b 为 x,y,2,1,1
如果 b 为:x,y,z 则 ReDim b 为 x,y,z,2,1,1

简单描述就是要确保 b 里面最后3个元素是2,1,1。允许扩充 b,但不允许最后重复出现2,1,1。即:2,1,1,2,1,1 是不允许的。

问题完毕!

对回答前辈们的要求:
1、其中的 2,1,1 只是本例中的假设数组尾部,请勿针对 1,2 的数字做文章!
2、请以最少变量、最简短的代码实现,复杂的方式则没必要回复了!

再次感谢!!!
...全文
581 44 打赏 收藏 转发到动态 举报
写回复
用AI写文章
44 条回复
切换为时间正序
请发表友善的回复…
发表回复
oshi002 2011-10-15
  • 打赏
  • 举报
回复
终于看到一个可以回复的帖子了。
lixiankun001 2009-05-18
  • 打赏
  • 举报
回复
mark!!!
嘿嘿。
Tiger_Zhao 2009-05-18
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 shier2817 的回复:]
诚如14楼前辈所言,14楼与15楼的都不是十分理想,因为我这个程序已经相当长了,所以不希望在这个小功能上有过长的代码和过多的过程!
[/Quote]
仔细看过代码没有?
我15楼的 CheckEnd() 代码 21 行,其它都是测试代码。
你给分的19楼代码甚至无法对应 LenSouByte < LenConByte 的情形。

代码不正确可以指出来,自己不仔细对待就别指望别人的帮助。
CqCoder 2009-05-18
  • 打赏
  • 举报
回复
UP
jacky2400 2009-05-18
  • 打赏
  • 举报
回复
有点难
hwb110 2009-05-17
  • 打赏
  • 举报
回复
Function GetArray(a() As Byte, b() As Byte) As Byte()

Dim n As Long, m As Long
Dim l As Long, k As Long
Dim i As Long, j As Long
Dim tmp() As Byte

n = UBound(a)
m = UBound(b)
k = n + m + 1
tmp = a
i = n
Do While i < k
If ArrayComp(tmp, b) Then
Exit Do
Else
i = i + 1
ReDim Preserve tmp(i)
l = m
For j = i To n + 1 Step -1
tmp(j) = b(l)
l = l - 1
Next
End If
Loop
GetArray = tmp

End Function

Function ArrayComp(a() As Byte, b() As Byte) As Boolean
Dim i As Long, j As Long
For i = UBound(a) - UBound(b) To UBound(a)
If a(i) <> b(j) Then Exit Function
j = j + 1
Next
ArrayComp = True
End Function

redhat2 2009-05-17
  • 打赏
  • 举报
回复
关注


.



人力资源

NaturalSBQ 2009-05-17
  • 打赏
  • 举报
回复
学习
kk324 2009-05-17
  • 打赏
  • 举报
回复
看下 学习下
iwifksfksjfkads 2009-05-17
  • 打赏
  • 举报
回复
学习
shaozhi_2724 2009-05-16
  • 打赏
  • 举报
回复
学习
vbman2003 2009-05-16
  • 打赏
  • 举报
回复
用CompMemory是可以的,不过字符比较和每个循环都ReDim Preserve明显效率不高...
vbman2003 2009-05-16
  • 打赏
  • 举报
回复
再改下:


Sub GetBytes(souByte() As Byte, conByte() As Byte)

Dim LenSouByte As Long, LenConByte As Long
Dim i As Long, j As Long
Dim Length As Long

LenSouByte = UBound(souByte)
LenConByte = UBound(conByte)

For i = LenSouByte - LenConByte To LenSouByte
j = i
Length = 0
Do While (souByte(j) = conByte(Length))
j = j + 1
Length = Length + 1
If j > LenSouByte Then Exit For
Loop
Next i
ReDim Preserve souByte(LenSouByte + (LenConByte + 1 - Length))
j = LenConByte
For i = UBound(souByte) To LenSouByte + 1 Step -1
souByte(i) = conByte(j)
j = j - 1
Next
End Sub

vbman2003 2009-05-16
  • 打赏
  • 举报
回复

'主函数
Sub GetBytes(souByte() As Byte, conByte() As Byte)

Dim LenSouByte As Long, LenConByte As Long
Dim i As Long, j As Long
Dim Length As Long

LenSouByte = UBound(souByte)
LenConByte = UBound(conByte)

For i = LenSouByte - LenConByte To LenSouByte
j = i
Length = 0
Do While (souByte(j) = conByte(Length)) And (j <= LenSouByte)
j = j + 1
Length = Length + 1
If j > LenSouByte Then Exit For
Loop
Next i
ReDim Preserve souByte(LenSouByte + (LenConByte + 1 - Length))
j = LenConByte
For i = UBound(souByte) To LenSouByte + 1 Step -1
souByte(i) = conByte(j)
j = j - 1
Next
End Sub

'Print结果
Sub PrintBytes(myByte() As Byte)
Dim i As Long
For i = 0 To UBound(myByte)
Debug.Print myByte(i);
Next
Debug.Print
End Sub



'测试
Private Sub Command1_Click()

Dim a() As Byte, b() As Byte

ReDim a(2)
a(0) = 3: a(1) = 2: a(2) = 1

ReDim b(2)
b(0) = 12: b(1) = 11: b(2) = 10
GetBytes b, a
PrintBytes b

ReDim b(2)
b(0) = 12: b(1) = 11: b(2) = 3
GetBytes b, a
PrintBytes b

ReDim b(2)
b(0) = 12: b(1) = 3: b(2) = 2
GetBytes b, a
PrintBytes b

ReDim b(2)
b(0) = 3: b(1) = 2: b(2) = 1
GetBytes b, a
PrintBytes b
End Sub


结果:
12 11 10 3 2 1
12 11 3 2 1
12 3 2 1
3 2 1
amanthul 2009-05-16
  • 打赏
  • 举报
回复
很好
cszhangwei 2009-05-16
  • 打赏
  • 举报
回复
dddd
twysbsn 2009-05-16
  • 打赏
  • 举报
回复
学习
qrhard 2009-05-16
  • 打赏
  • 举报
回复
都很强的!学习学习!
cicilool 2009-05-16
  • 打赏
  • 举报
回复
jf
rejoice4444 2009-05-16
  • 打赏
  • 举报
回复
。。。。。。。
加载更多回复(23)

7,763

社区成员

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

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