有没有简单的方法,得知一个整形数组(最少30个数)中,连续的数(最少四个数,数值相同或递增,递减)所在的位置;
如:1,1,2,3,4,9,8,4,5,9,9,9,9 中
1234是连续递增,9999是相同得数,他们的位置分别在2-5,10-13
谢谢大家了!

解决方案 »

  1.   

    Private Sub Command1_Click()
        Dim I As Integer
        Dim MyInt(1 To 13) As Integer
        '模拟数据
        MyInt(1) = 1
        MyInt(2) = 1
        MyInt(3) = 2
        MyInt(4) = 3
        MyInt(5) = 4
        MyInt(6) = 9
        MyInt(7) = 8
        MyInt(8) = 4
        MyInt(9) = 5
        MyInt(10) = 9
        MyInt(11) = 9
        MyInt(12) = 9
        MyInt(13) = 9
        
        For I = 1 To UBound(MyInt) - 3
            If MyInt(I) = MyInt(I + 1) - 1 And MyInt(I) = MyInt(I + 2) - 2 And MyInt(I) = MyInt(I + 3) - 3 Then
                MsgBox "位置" & I & "-" & I + 3 & "数值递增"
            ElseIf MyInt(I) = MyInt(I + 1) And MyInt(I) = MyInt(I + 2) And MyInt(I) = MyInt(I + 3) Then
                MsgBox "位置" & I & "-" & I + 3 & "数值相同"
            End If
        Next
    End Sub
      

  2.   


    只能遍历了,还能怎样?不过,可能在遍历时,先剔除掉明显不符合的,以加快速度,以一楼的代码为例,加一个判断:
    For I = 1 To UBound(MyInt) - 3
        if myint(i)=myint(i+1)-1 or myint(i)=myint(i+1) then 
            If MyInt(I) = MyInt(I + 1) - 1 And MyInt(I) = MyInt(I + 2) - 2 And MyInt(I) = MyInt(I + 3) - 3 Then
                MsgBox "位置" & I & "-" & I + 3 & "数值递增"
            ElseIf MyInt(I) = MyInt(I + 1) And MyInt(I) = MyInt(I + 2) And MyInt(I) = MyInt(I + 3) Then
                MsgBox "位置" & I & "-" & I + 3 & "数值相同"
            End If
        end if
    Next
      

  3.   

    For I = 1 To UBound(MyInt) - 3
    j=1
    do while j<=3
     if myint(i+j)=myint(i+j+1)-1 then
      exit do
       if j=3 then
       msgbox i
      endif
     end if 
     j=j+1
     
    loop
    Next!!(没完善)
    就这个意思吧,可好!???
      

  4.   

    上面的exit do位置写错了
    应该在else
    中写!!