求小算法:一组数中找出连续14个数相邻大小交替的数

lanhai96 2016-05-08 11:04:37
给定一组数:
如:
948
1001
985
1012
1015
1011
1014
1009
1011
1004
1012
1007
1019
1005
1012
1003
1019
1016
1024
找出这组数中 连续14个数,它们相邻、并且大小交替出现。
如下图


跪求大神指点。.........
...全文
188 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tiger_Zhao 2016-05-09
  • 打赏
  • 举报
回复
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
一如既往哈 2016-05-09
  • 打赏
  • 举报
回复
这个得自己写。参考思路: 定义一个integer变量,如果后面的数比前面大+1,反之则-1,随时判断变量值范围【-1,1】和数量就可以了
lanhai96 2016-05-09
  • 打赏
  • 举报
回复
试了,两个都不错,这个要画图判断,我先试试那个最好,完了给分,谢谢。
of123 2016-05-09
  • 打赏
  • 举报
回复
利用 ListBox 和随机数的一个方法。在窗体上添加一个 Sorted = True 的 ListBox,Visible 可以 = False。
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

7,762

社区成员

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

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