Advertisement
Guest User

Nico 1

a guest
Oct 10th, 2015
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.01 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. function free_positions($cua = array(), $first, $last, $MAX){
  5.     $free = 0;
  6.  
  7.     if($first < $last){
  8.  
  9.         if($first != 1){    // Significa que hay huecos desde el principio
  10.  
  11.             $free = $free + ($first - 1);
  12.  
  13.         }
  14.  
  15.         if($last != 1){    // Significa que hay huecos desde hasta el final
  16.  
  17.             $free = $free + ($MAX - $last);
  18.  
  19.         }
  20.  
  21.     } else {
  22.  
  23.         $free = $first - $last - 1;
  24.  
  25.     }
  26.  
  27.  
  28.     return $free;
  29. }
  30.  
  31. $MAX = 10;
  32.  
  33. $cua = array(
  34.     1 => '',
  35.     2 => '',
  36.     3 => 'ocupat',
  37.     4 => 'ocupat',
  38.     5 => 'ocupat',
  39.     6 => '',
  40.     7 => '',
  41.     8 => '',
  42.     9 => '',
  43.     10 => ''
  44. );
  45.  
  46. $first = 3;
  47. $last = 5;
  48.  
  49. echo free_positions($cua, $first, $last, $MAX);
  50.  
  51. $MAX = 10;
  52.  
  53. $cua = array(
  54.     1 => 'ocupat',
  55.     2 => 'ocupat',
  56.     3 => '',
  57.     4 => '',
  58.     5 => '',
  59.     6 => '',
  60.     7 => '',
  61.     8 => 'ocupat',
  62.     9 => 'ocupat',
  63.     10 => 'ocupat'
  64. );
  65.  
  66. $first = 8;
  67. $last = 2;
  68.  
  69. echo free_positions($cua, $first, $last, $MAX);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement