7,762
社区成员
发帖
与我相关
我的任务
分享
Option Explicit
Sub Main()
Const FIND_COUNT As Long = 14
Dim a() As Variant
Dim lLastTrend As Long '上个变化趋势'
Dim lThisTrend As Long '当前变化趋势'
Dim bIsAddMatch As Boolean '当前是否符合交替变化(至少是第3个了)'
Dim lMatchCount As Long '符合的长度'
Dim i As Long
Dim j As Long
a = Array(948, 1001, 985, 1012, 1015, 1011, 1014, 1009, 1011, 1004, 1012, 1007, 1019, 1005, 1012, 1003, 1019, 1016, 1024)
For i = LBound(a) + 1 To UBound(a)
lThisTrend = Sgn(a(i) - a(i - 1))
If lThisTrend = 0 Then
bIsAddMatch = False
Else
bIsAddMatch = ((lThisTrend + lLastTrend) = 0)
End If
If bIsAddMatch Then
lMatchCount = lMatchCount + 1
If lMatchCount = FIND_COUNT Then
Exit For
End If
ElseIf lThisTrend = 0 Then
lMatchCount = 0
Else
lMatchCount = 2
End If
lLastTrend = lThisTrend
Next
Debug.Print "MatchCount = " & lMatchCount
For j = i - lMatchCount + 1 To i
Debug.Print CStr(a(j));
If j < i Then Debug.Print ", ";
Next
Debug.Print
End Sub
MatchCount = 14
1012, 1015, 1011, 1014, 1009, 1011, 1004, 1012, 1007, 1019, 1005, 1012, 1003, 1019
Private Sub Command1_Click()
Dim a() As Variant, i As Integer, m As Integer, n As Integer
a = Array(948, 1001, 985, 1012, 1015, 1011, 1014, 1009, 1011, 1004, 1012, 1007, 1019, 1005, 1012, 1003, 1019, 1016, 1024)
'put data into a sorted list
List1.Clear
For i = 0 To UBound(a)
List1.AddItem Right("0000" & a(i), 4)
Next i
'cut it to 14 numbers
Do Until List1.ListCount = 14
List1.RemoveItem 0
If List1.ListCount = 14 Then Exit Do
List1.RemoveItem List1.ListCount - 1
Loop
'get the data
Randomize
For i = 1 To 6
'get a smaller number and a bigger number
m = Rnd * (List1.ListCount / 2)
n = Rnd * (List1.ListCount / 2) + List1.ListCount / 2
If n > List1.ListCount - 1 Then n = List1.ListCount - 1
Debug.Print Val(List1.List(m))
Debug.Print Val(List1.List(n))
List1.RemoveItem n
List1.RemoveItem m
Next i
Debug.Print Val(List1.List(0))
Debug.Print Val(List1.List(1))
List1.RemoveItem 1
List1.RemoveItem 0
End Sub