Advertisement
Garey

NikolayDONG

Apr 9th, 2019
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.55 KB | None | 0 0
  1. <?php
  2.  
  3. /*! Input Count */
  4. $countNum = intval(readline());
  5.  
  6. /*! Numbers array & input array */
  7. $numbers = array('odd' => ['min' => 'No', 'max' => 'No', 'sum' => 0.0], 'even' => ['min' => 'No', 'max' => 'No', 'sum' => 0.0]);
  8. $input = array();
  9.  
  10. /*! If $countNum has value different than 0 or null */
  11. if($countNum) {
  12.  
  13.     /*! Insert values into temporary array */
  14.     for($index = 0; $index < $countNum; $index++) {
  15.         $number = floatval(readline());
  16.         array_push($input, $number);
  17.  
  18.         /*! Set Min and Max */
  19.         $numbers[$number & 1 ? 'odd' : 'even']['min'] = $number;
  20.         $numbers[$number & 1 ? 'odd' : 'even']['max'] = $number;
  21.     }
  22.  
  23.     /*! Organize the main array */
  24.     for($index = 0; $index < $countNum; $index++) {
  25.  
  26.         /*! Set type */
  27.         $type = ($input[$index] & 1 ? 'odd' : 'even');
  28.  
  29.         /*! Increment the sum */
  30.         $numbers[$type]['sum'] += $input[$index];
  31.        
  32.         /*! Check for Min */
  33.         if($input[$index] < $numbers[$type]['min'])
  34.             $numbers[$type]['min'] = $input[$index];
  35.  
  36.         /*! Check for Max */
  37.         if($input[$index] > $numbers[$type]['max'])
  38.             $numbers[$type]['max'] = $input[$index];
  39.     }
  40. }
  41.  
  42. printf('Odd Sum: ' . $numbers['odd']['sum'] . PHP_EOL);
  43. printf('Odd Min: ' . $numbers['odd']['min'] . PHP_EOL);
  44. printf('Odd Max: ' . $numbers['odd']['max'] . PHP_EOL);
  45.  
  46. printf('Even Sum: ' . $numbers['even']['sum'] . PHP_EOL);
  47. printf('Even Min: ' . $numbers['even']['min'] . PHP_EOL);
  48. printf('Even Max: ' . $numbers['even']['max'] . PHP_EOL);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement