1

我有 7 个数值,零或更大,按一周中的每一天排序。少至一个或多于所有的值可能大于零。我需要一种方法来确定 >0 值是否出现在单个序列中。

例如:

0, 8, 8, 0, 0, 0, 0

将是一个序列,而

8, 0, 8, 8, 8, 0, 0

有两个单独的序列,因此不符合条件。如果整个分组中只有一个大于零的值,我也希望它被视为一个合格序列。

我从一个函数开始,它接受 7 个变量并根据每个变量是零还是非零将它们填充到一个布尔数组中——0 为假,> 0 为真,这给了我类似的东西

{false, true, true, false, false, false, false}

我现在需要确定该数组的内容是否包含上述限定条件的限定序列。有任何想法吗?

对于它的价值,这是我迄今为止的功能:

Public Function IsConcurrent(D1, D2, D3, D4, D5, D6, D7) As Boolean
  Dim arr(6) As Single
  arr(0) = D1
  arr(1) = D2
  arr(2) = D3
  arr(3) = D4
  arr(4) = D5
  arr(5) = D6
  arr(6) = D7

  Dim concurrent As Boolean = False
  If CSng(arr(0)) > 0 Then concurrent = True
  For k = 2 To 7
    If arr(k - 1) = arr(k - 2) Then
    Else
    End If
  Next

  Return concurrent
End Function
4

2 回答 2

1

...我使用Lists而不是数组编写了这个,但结构将是相同的。

我是这样做的:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim x As List(Of Integer) = {0, 0, 8, 8, 8, 0, 0}.ToList

    Dim Test As Boolean = LookForSingleSequence(x)
End Sub


Private Function LookForSingleSequence(ByVal MyList As List(Of Integer)) As Boolean

    Dim i As Integer
    Dim RetVal As Boolean = False
    Dim SeqEnded As Boolean = False

    For i = 0 To MyList.Count - 1
        If MyList(i) > 0 Then
            If SeqEnded = True Then
                RetVal = False
                Exit For
            Else
                RetVal = True
            End If
        End If

        If RetVal = True And MyList(i) <= 0 Then SeqEnded = True
    Next

    Return RetVal

End Function

希望这可以帮助!!

于 2013-02-26T15:46:26.720 回答
1

这是 LINQ 解决方案 - 代码行更少,希望更容易理解:

Sub Main()
  Dim lst As New List(Of Integer)({0, 8, 8, 0, 0, 0, 0})
  Console.WriteLine(IsConcurrent(lst)) 'True
  lst = New List(Of Integer)({8, 0, 8, 8, 8, 0, 0})
  Console.WriteLine(IsConcurrent(lst)) 'False
  Console.ReadLine()
End Sub

Private Function IsConcurrent(ByVal lst As List(Of Integer)) As Boolean
  Dim elementsAfterZeros = lst.SkipWhile(Function(x) x <= 0)
  If elementsAfterZeros.Count = 0 Then Return False 'we only have zeros
  Dim elementsInSecondGroupOfNonZeros = elementsAfterZeros.
                                        SkipWhile(Function(x) x > 0).
                                        SkipWhile(Function(x) x <= 0)
  If elementsInSecondGroupOfNonZeros.Count = 0 Then Return True
  Return False
End Function
于 2013-02-26T16:12:26.843 回答