Cherry83

8.Trip Length v.2

Feb 9th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.66 KB | None | 0 0
  1. <?php
  2.  
  3. function Distance($x1, $y1, $x2, $y2)
  4. {
  5.     return sqrt(pow($x1 - $x2, 2) + pow($y1 - $y2, 2));
  6. }
  7.  
  8. list($x1, $y1, $x2, $y2, $x3, $y3) = explode(", ", trim(fgets(STDIN)));
  9. /*
  10. preg_match_all("/(-?\d*\.{0,1}\d+)/", trim(fgets(STDIN)), $input);
  11. list($x1, $y1, $x2, $y2, $x3, $y3) = $input[0];
  12. */
  13.  
  14. $d3 = Distance($x1, $y1, $x2, $y2);
  15. $d1 = Distance($x2, $y2, $x3, $y3);
  16. $d2 = Distance($x1, $y1, $x3, $y3);
  17.  
  18. $trip1 = $d1 + $d3;
  19. $trip2 = $d1 + $d2;
  20. $trip3 = $d2 + $d3;
  21.  
  22. if ($trip3 < $trip2 && $trip3 < $trip1) {
  23.     echo "2->1->3: " . $trip3;
  24. } elseif ($trip2 < $trip1) {
  25.     echo "1->3->2: " . $trip2;
  26. } else {
  27.     echo "1->2->3: " . $trip1;
  28. }
Add Comment
Please, Sign In to add comment