這是一個非常簡單的數學問題。如果我有兩個號碼的範圍,什麼是檢查最簡單,最有效的方式,如果他們發生衝突,如:檢測兩個數字範圍是否衝突
10-20 and 11-14 // clash as B is contained in A
11-15 and 20-22 // don't clash
24-26 and 20-30 // clash as A is contained in B
15-25 and 20-30 // clash as they overlap at each end
我現在有這個爛攤子,但必須有一個更簡單的做此項檢查方式:
$clash = ($b1 >= $a1 && $b1 <= $a2)
|| ($b2 >= $a1 && $b2 <= $a2)
|| ($a1 >= $b1 && $a1 <= $b2)
|| ($a2 >= $b1 && $a2 <= $b2);
我只是想發佈:-) +1更快。 – 2009-09-08 22:11:46
或者沒有否定:'$ clash =($ b1 <= $ a2)&&($ a1 <= $ b2)' – sth 2009-09-08 22:19:16
謝謝,那很好。 – 2009-09-08 22:28:17