Cherry83

8.Trip Length v.3

Feb 9th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.54 KB | None | 0 0
  1. <?php
  2.  
  3. function Distance($x1, $y1, $x2=0, $y2=0)
  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. $d3 = Distance($x1, $y1, $x2, $y2);
  11. $d1 = Distance($x2, $y2, $x3, $y3);
  12. $d2 = Distance($x1, $y1, $x3, $y3);
  13.  
  14. $trip1 = $d1 + $d3;
  15. $trip2 = $d1 + $d2;
  16. $trip3 = $d2 + $d3;
  17.  
  18. if ($trip3 < $trip2 && $trip3 < $trip1) {
  19.     echo "2->1->3: " . $trip3;
  20. } elseif ($trip2 < $trip1) {
  21.     echo "1->3->2: " . $trip2;
  22. } else {
  23.     echo "1->2->3: " . $trip1;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment