檢測兩個數字范圍是否沖突。

[英]Detecting if two number ranges clash


This is hopefully a very simple maths question. If I have two number ranges, what is the simplest and most efficient way to check if they clash, eg:

這是一個很簡單的數學問題。如果我有兩個數字范圍,最簡單、最有效的檢查它們是否沖突的方法是什么?

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

I currently have this mess, but there must be a much simpler way to do this check:

我目前的情況一團糟,但一定有更簡單的方法來做這個檢查:

$clash = ($b1 >= $a1 && $b1 <= $a2)
    || ($b2 >= $a1 && $b2 <= $a2)
    || ($a1 >= $b1 && $a1 <= $b2)
    || ($a2 >= $b1 && $a2 <= $b2);

5 个解决方案

#1


11  

Well, first make sure you have well-ordered pairs (probably a good idea anyway, depending on what you plan to do with them):

首先,確保你有有序的配對(不管怎樣,這可能是個好主意,取決於你打算怎么做):

if($a1 > $a2) {
    // swap $a1 and $a2
    $temp = $a1;
    $a1 = $a2;
    $a2 = $temp;
}
if($b1 > $b2) {
    // swap $b1 and $b2
    $temp = $b1;
    $b1 = $b2;
    $b2 = $temp;
}

Then you should be able to simplify to:

然后你就可以化簡為:

$clash = ($a2 <= $b1) || ($a1 >= $b2);

Edit: Whoops, got that test backwards! Try:

編輯:哎呀,把測試搞反了!試一試:

$clash = !(($a2 <= $b1) || ($a1 >= $b2));

#2


7  

I think it should be as simple as this:

我認為應該這么簡單:

clash = A_LOW <= B_HIGH AND A_HIGH >= B_LOW

#3


3  

example:
10 - 20
4 - 11 // this will clash with above
1 - 5 // this will clash with above
40 - 50
store your numbers in 2 arrays, say
x_array = array(10,4,11,40);
y_array = array(20,11,5,50);

示例:10 - 20 4 - 11 // /這將與上面的1 - 5 // /發生沖突,與上面的40 - 50沖突,將數字存儲在兩個數組中,例如x_array = array(10,4,11,40);y_array =數組(5 20日,11日,50);

asort($x_array); // sort only the first x range array values and maintain the index
$max_val = -1;
$last_index = 0;
foreach($x_array as $each_index => $each_x){
  // get the y corresponding value
  $this_y = $y_array[$each_index];
  echo "$this_y < $max_val";
  if($each_x > $max_val && $this_y > $max_val){
    $max_val = $this_y;
  }
  else{
    $last_x = $x_array[$last_index];
    $last_y = $y_array[$last_index];
    echo "Error: Overlap between: ($each_x,$this_y) and ($last_x,$last_y)";
  }
  $last_index = $each_index;
}

帶點($ x_array);//只對第一個x范圍數組值進行排序,並保持索引$max_val = -1;美元last_index = 0;foreach($x_array as $each_index => $each_x){// /獲得y對應值$this_y = $y_array[$each_index];回聲“this_y < max_val美元”;如果($ each_x > max_val & & $ this_y > max_val美元){ max_val = this_y美元;{$last_x = $x_array[$last_index];last_y = y_array美元[$ last_index];回聲“錯誤:重疊:(each_x,this_y美元)和(last_x,last_y美元)”;} last_index = each_index美元;}

#4


2  

The ranges DO NOT clash if and only if $a2 <= $b1 or $a1 >= $b2 (assuming that ranges are given as ordered pairs). Now negate the condition.

當且僅當$a2 <= $b1或$a1 >= $b2時,范圍不會發生沖突(假設范圍為有序對)。現在否定條件。

#5


0  

What you're looking for is the intersection of the arrays. http://us3.php.net/array_intersect

你要找的是數組的交集。http://us3.php.net/array_intersect

Basically,

基本上,

$intersect = array_intersect($arr1, $arr2);
$clash = (count($intersect) > 0);

If any elements are in both $arr1 and $arr2, then $intersect will list those values. The count() call returns 1 (or more), so doing > 0 gives you if $arr1 and $arr2 have any similar elements.

如果任何元素都在$arr1和$arr2中,那么$intersect將列出這些值。count()調用返回1(或更多),因此,如果$arr1和$arr2有任何類似的元素,那么執行> 0就會得到。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/09/08/192a00ddf2b1c1c39e12497b622bbaf6.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com