0

While having a loop with 0 1 2 3 4 5 0 1 2 3 ... there are two directions for instance from number 4 forwarding 4 5 0 1 2 3 ... and backward 4 3 2 1 0 5 ... . How would the if algorithm for detecting of forwarding or backward look like if values are like actual 2 next 3, or actual 5 next 0.

I'm looking for a simple if that will check

if (actual > next && ?jump from last/first?) { 
  forwarding
} else {
  backward
}

Any ideas how to detect forward or backward?

4

1 に答える 1

0

これを行う1つの方法は次のとおりです。

$forward = ($actual < $next);
if (abs($actual-$next) != 1) $forward = !$forward;

actualがnextより小さい場合は、順方向であると想定して開始します。次に、実際の値との値の違いを確認します。違いが1 つでない場合は、境界線上にいるため、結果は予想とは逆になります。

于 2013-07-17T21:04:10.767 に答える