有两个数组,a(6),b(6),要比较这两个数组中的数是不是完相同,也许有的数字不在同一位置上,该怎么做啊,我写了一个小算法,不知道还能不能优化
dim num(6)
for i=0 to 5
for j=0 to 5
if a(i)=b(j) then
num(i)=num(i)+1
exit for
end if
next j
next i
通过num(i)的情况判断是否相等,诸位还有没有什么更好的办法,谢谢了
...全文
43140打赏收藏
求比较一致性算法
有两个数组,a(6),b(6),要比较这两个数组中的数是不是完相同,也许有的数字不在同一位置上,该怎么做啊,我写了一个小算法,不知道还能不能优化 dim num(6) for i=0 to 5 for j=0 to 5 if a(i)=b(j) then num(i)=num(i)+1 exit for end if next j next i 通过num(i)的情况判断是否相等,诸位还有没有什么更好的办法,谢谢了
Private Sub Command1_Click()
Dim tActiveTable As New clsActiveTable_Long
For tIndex = 10000 To 20000
tActiveTable.Index = tIndex
tActiveTable.Value = tIndex
DoEvents
Text1.Text = tActiveTable.Size
Next
End Sub
Private Sub Command2_Click()
Dim tA() As Long
Dim tB() As Long
ReDim tA(3)
ReDim tB(3)
Function Longs_MatchCheck(ByRef pLongsA() As Long, ByRef pLongsB() As Long, Optional ByVal pMode As Longs_MatchCheck_Mode = 3, Optional ByVal pValueMax As Long = 9999, Optional ByVal pValueMin As Long = 0)
Dim tOutBool As Boolean
Dim tLongs_Min As Long
Dim tLongs_Max As Long
Select Case pMode
Case 0, 1, 2
tLongs_Max = pValueMax
tLongs_Min = pValueMin
Case 3
tLongs_Min = Longs_FindMin(pLongsA()): tLongs_Min = Longs_FindMin(pLongsB(), tLongs_Min)
tLongs_Max = Longs_FindMax(pLongsA()): tLongs_Max = Longs_FindMax(pLongsB(), tLongs_Max)
End Select
Dim tMode As Boolean
Dim tValueLength As Long
Dim tLongsLength As Long
tMode = (tValueLength > tLongsLength) Or pMode = cmDynamicTable
If tMode Then
tOutBool = Longs_MatchCheck_DynamicTable(pLongsA(), pLongsB())
Else
tOutBool = Longs_MatchCheck_StaticTable(pLongsA(), pLongsB(), tLongs_Max, tLongs_Min)
End If
Longs_MatchCheck = tOutBool
End Function
Function Longs_MatchCheck_DynamicTable(ByRef pLongsA() As Long, ByRef pLongsB() As Long) As Boolean
Dim tOutBool As Boolean
Dim tIndex As Long
Dim tIndex_Start As Long
Dim tIndex_End As Long
For tIndex = tIndex_Start To tIndex_End
tActiveTable.Index = pLongsB(tIndex)
tOutBool = tOutBool And CBool(tActiveTable.Value)
If Not tOutBool Then Exit For
Next
Longs_MatchCheck_DynamicTable = tOutBool
End Function
Function Longs_MatchCheck_StaticTable(ByRef pLongsA() As Long, ByRef pLongsB() As Long, Optional ByVal pValueMax As Long = 9999, Optional ByVal pValueMin As Long = 0) As Boolean
Dim tOutBool As Boolean
Dim tTable() As Boolean
ReDim tTable(pValueMin To pValueMax)
Dim tIndex As Long
Dim tIndex_Start As Long
Dim tIndex_End As Long
Function MatchingNumberGetByMap(ByRef pMapA() As Long, ByRef pMapB() As Long) As Long
'通过直方图计算不匹配数量。
Dim tOutNumber As Long
Dim tIndex As Long
For tIndex = LBound(pMapA()) To UBound(pMapA())
tOutNumber = tOutNumber + Abs(pMapA(tIndex) - pMapB(tIndex))
Next
MatchingNumberGetByMap = tOutNumber
End Function
Sub Bytes_MatchingCheck3_MapGet(ByRef pArrayA() As Byte, ByRef pArrayB() As Byte, ByRef pOutMapA() As Long, ByRef pOutMapB() As Long)
'获得数组直方图。
'对于文本来说,本方法最为适用。
Dim tOutMapA() As Long
Dim tOutMapB() As Long
Dim tArrayA_Max As Byte
Dim tArrayB_Max As Byte
Dim tArrayA_Min As Byte
Dim tArrayB_Min As Byte
For tIndexLevel1 = 0 To tArrayLength
For tIndexLevel2 = tIndexLevel1 To tArrayLength
'同步排序
Bytes_SwapByMaxLeft pArrayA(tIndexLevel1), pArrayA(tIndexLevel2)
Bytes_SwapByMaxLeft pArrayB(tIndexLevel1), pArrayB(tIndexLevel2)
Next
'同步性检测
tOutBool = tOutBool And (pArrayA(tIndexLevel1) = pArrayB(tIndexLevel1))
If Not tOutBool Then
'不同步立即退出
Exit For
End If
Next
Bytes_MatchingCheck2 = tOutBool
End Function
Function Bytes_MatchingCheck(ByRef pArrayA() As Byte, ByRef pArrayB() As Byte) As Boolean
Dim tOutBool As Boolean
Dim tArrayLength As Long
tArrayLength = UBound(pArrayA())
Dim tIndex As Long
Dim tSumValue As Long
For tIndex = 0 To tArrayLength
tSumValue = tSumValue + (CLng(pArrayA(tIndex)) - CLng(pArrayB(tIndex)))
Next
tOutBool = Not CBool(tSumValue)
Bytes_MatchingCheck = tOutBool
End Function
Sub Bytes_ValueBoundGet(ByRef pBytes() As Byte, ByRef pMax As Byte, ByRef pMin As Byte)
Dim tBytes_Length As Long
tBytes_Length = UBound(pBytes())
Dim tIndex As Long
pMax = pBytes(tIndex)
pMin = pBytes(tIndex)
For tIndex = 0 To tBytes_Length
If pMax < pBytes(tIndex) Then pMax = pBytes(tIndex)
If pMin > pBytes(tIndex) Then pMin = pBytes(tIndex)
Next
End Sub
Sub Bytes_SwapByMaxLeft(ByRef pA As Byte, ByRef pB As Byte)
If pA < pB Then
Bytes_Swap pA, pB
End If
End Sub
Sub Bytes_Swap(ByRef pA As Byte, ByRef pB As Byte)
Dim tT As Byte
tT = pA: pA = pB: pB = tT
End Sub