2016-11-28 7 views
0

私の関数は多次元配列を反復処理し、条件を満たす必要があります。配列には1と0が含まれます。 1対1の場合は、少なくとも4個と最大9個まで連続して印刷することができます。つまり、1111または111111では11個または11111111111が許可されません.1個の間に12個以上のゼロがなければなりません。例えば、1111000000000000111111です。 36の連続する0になる。後者は私が追加した制約であり、実行を停止していないようです。これは私が、私はこの機能を追加したので、コードがまだ実行されている理由を理解することに興味が平日= 7とmaxconsecutivehours = 36私のコードにこの関数を追加したので、実行を停止していません。 [VBA]

Dim i, j, m, counter1, counter2, counter3, counterA, remainder As Double 

For i = 1 To UBound(posSolution, 1) 
    counter1 = 0 
    counter2 = 0 
     For j = 1 To UBound(posSolution, 2) 
      If posSolution(i, j) = 0 Then 
       Do Until posSolution(i, j) = 1 
        counter1 = counter1 + 1 
       Loop 
        counter2 = counter2 + 1 
         If counter1 >= maxConsecutiveHours Then 
          j = j + 24 * (workDays - counter2) 
          counter1 = 0 
          counter2 = 0 
         Else 
          remainder = maxConsecutiveHours - counter1 
          counter3 = j 
           Do Until posSolution(i, counter3) = 1 
            counter3 = counter3 - 1 
           Loop 
            Do While posSolution(i, counter3) = 1 
             counter3 = counter3 - 1 
             counterA = counterA + 1 
            Loop 
             If (counterA - remainder) >= minWorkHours Then 
              For m = (counter3 + counterA - remainder) To counter3 + counterA 
               posSolution(i, m) = 0 
              Next m 
              j = m + 24 * (workDays - counter2) 
              counter1 = 0 
              counter2 = 0 
              counter3 = 0 
              counterA = 0 
             End If 
         End If 
      End If 
     Next j 
Next i 

よう名簿ツールです。

答えて

1

コードのこの特定のビットは、出口決してでき:

Do Until posSolution(i, j) = 1 
    counter1 = counter1 + 1 
Loop 

posSolution(i, j)の値は、ループ内で変更されることはないので、それは1に等しくない場合、それがオーバーフローするまで、これを繰り返しcounter1をインクリメントします。あなたはそれが暗黙のうちにここVariantとして宣言している:

Dim i, j, m, counter1, counter2, counter3, counterA, remainder As Double 

のみremainderは明示的にDoubleある - 他のすべてがVariantです。それなるまでcounter1Variantあるので

Dim i As Double, j As Double, m As Double, etc... 

、それは繰り返し昇進します:あなたは、同じ行に複数の宣言を置く場合は、それぞれが1のための型を指定する必要がありますDoubleとなり、オーバーフローする前に精度が低下します。

関連する問題