7,762
社区成员
发帖
与我相关
我的任务
分享
Option Explicit
Private Sub Command1_Click()
Dim tStrings() As String
Dim tValues() As Long
Dim tValues_Length As Long
Dim tValues_Index As Long
Dim tSpaces() As Long '全值间隔表(编码格式)
Dim tFilterSpaces() As Long '特定值的过滤输出间隔表
Dim tFilterSpaces_Index As Long
Dim tFilterSpaces_Length As Long
Dim tFilterValue As Long
tStrings() = Split("1 2 3 3 6 5 4 4 1 6", " ")
tFilterValue = 1 '过滤输出的目标值
tValues_Length = UBound(tStrings())
ReDim tValues(tValues_Length)
For tValues_Index = 0 To tValues_Length
tValues(tValues_Index) = CLng(tStrings(tValues_Index))
Next
tSpaces() = SpacesGetByValues(tValues()) '建立全值间隔表
tFilterSpaces() = SpacesFilterOutByValue(tSpaces(), 1) '过滤输出目标值的间隔表
tFilterSpaces_Length = UBound(tFilterSpaces())
Text1.Text = ""
For tFilterSpaces_Index = 0 To tFilterSpaces_Length
Text1.Text = Text1.Text & " " & tFilterSpaces(tFilterSpaces_Index)
Next
End Sub
Function SpacesFilterOutByValue(ByRef pSpaces() As Long, ByVal pValue As Long, Optional ByVal pValue_Min As Long = 1, Optional ByVal pValue_Max = 7) As Long()
'过滤输出特定值的间隔表
Dim tOut_Value_Spaces() As Long
Dim tOut_Value_Length As Long
Dim tValue_Bound As Long
Dim tValue_Absolute As Long
Dim tValue_Space As Long
Dim pSpaces_Index As Long
Dim pSpaces_Length As Long
tValue_Bound = pValue_Max - pValue_Min + 1
pSpaces_Length = UBound(pSpaces())
ReDim tOut_Value_Spaces(pSpaces_Length)
For pSpaces_Index = 0 To pSpaces_Length
'解码tValue_Space和tValue_Absolute解码
tValue_Space = pSpaces(pSpaces_Index) \ tValue_Bound
tValue_Absolute = pSpaces(pSpaces_Index) Mod tValue_Bound
If pValue = tValue_Absolute + pValue_Min Then
tOut_Value_Spaces(tOut_Value_Length) = tValue_Space
tOut_Value_Length = tOut_Value_Length + 1
End If
Next
ReDim Preserve tOut_Value_Spaces(tOut_Value_Length - 1)
SpacesFilterOutByValue = tOut_Value_Spaces()
End Function
Function SpacesGetByValues(ByRef pValues() As Long, Optional ByVal pValue_Min As Long = 1, Optional ByVal pValue_Max = 7) As Long()
'产生一个间隔表,它记录了每个数值(数值记录时以0开始的绝对值表示)在列表中出现时距上一次出现时的间隔次数。
Dim tOut_Value_Spaces() As Long
Dim tValues_Index As Long
Dim tValues_Length As Long
Dim tValue_Bound As Long
Dim tValue_Absolute As Long '绝对值
Dim tValue_Space As Long '间隔值
Dim tValue_HashList() As Long
Dim tValue_HashList_Length As Long
Dim tList_Length As Long
tValue_Bound = pValue_Max - pValue_Min + 1
tValue_HashList_Length = tValue_Bound - 1
ReDim tValue_HashList(tValue_HashList_Length)
tValues_Length = UBound(pValues())
ReDim tOut_Value_Spaces(tValues_Length)
For tValues_Index = 0 To tValues_Length
tValue_Absolute = pValues(tValues_Index) - pValue_Min
tValue_Space = tValues_Index - tValue_HashList(tValue_Absolute) + 1
'将tValue_Space和tValue_Absolute编码在一起
tOut_Value_Spaces(tValues_Index) = tValue_Space * tValue_Bound + tValue_Absolute
tValue_HashList(tValue_Absolute) = tValues_Index + 1
Next
SpacesGetByValues = tOut_Value_Spaces()
End Function