Java Learning (10): Finding the closest point to the problem

Source: Internet
Author: User

1 Importjava.util.ArrayList;2 Importjava.util.Collections;3 Importjava.util.List;4 ImportJava.util.Scanner;5 6  Public classMain {7 8      Public Static voidMain (string[] args) {9Scanner Scanner =NewScanner (system.in);Ten         intN =-1; One          while(N! = 0) { AN =scanner.nextint (); -List<point> points =NewArraylist<>(); -              for(inti = 0; i < N; i++) { thePoint point =NewPoint (); - Point.setx (Scanner.nextdouble ()); - point.sety (Scanner.nextdouble ()); - Points.Add (point); +             } -             if(n!=0){ +                 DoubleMin =mindistance (points); ASystem.out.printf ("%.2f", min); at             }     -         } -     } -  -      Public Static DoubleMindistance (list<point>points) { -         intn =points.size (); in         if(N < 2) -             returnInteger.max_value; to         if(n = = 2) { +             DoubleDistance = distance (points.get (0), Points.get (1)); -             returndistance; the         } *Collections.sort (points);//arrange the points in X-coordinates . $         //The dividing line is half the middle two point x coordinate.Panax Notoginseng         Doublem = (Points.get (points.size ()/2-1). GetX () + Points.get (Points.size ()/2). GetX ())/2; -         //Dividing by x = m into two point sets theList<point> leftpoints =NewArraylist<>(); +List<point> rightpoints =NewArraylist<>(); ALeftpoints.addall (points.sublist (0, Points.size ()/2)); theRightpoints.addall (Points.sublist (Points.size ()/2, Points.size ())); +         //get the shortest distance between the left and right two point sets -         DoubleLeftmin =mindistance (leftpoints); $         DoubleRightmin =mindistance (rightpoints); $  -         //get the shortest distance -         DoubleMin =math.min (Leftmin, rightmin); the  -         //creating P1 Point sets and P2 point setsWuyiList<point> P1 =NewArraylist<>(); theList<point> P2 =NewArraylist<>(); -          for(Point point:points) { Wu             if(Point.getx () >= (m-min) && Point.getx () <m) { - P1.add (point); About             } $             if(Point.getx () > M && point.getx () <= m +min) { - P2.add (point); -             } -         } A  +         DoubleMin2 =Integer.max_value; the         Doubledistance; -         BooleanFlag1, Flag2; $         if(P1! =NULL&& P2! =NULL) { the              for(Point point:p1) { the                  for(Point point2:p2) { theFlag1 = (point2.gety () >= (Point.gety ()-min) && point2.gety () <=point.gety ()); theFlag2 = (point2.gety () >= point.gety () && point2.gety () <= (point.gety () +min)); -                     if(Flag1 | |Flag2) { inDistance =Distance (point,point2); the                         if(Distance <min2) { theMin2 =distance; About                         } the  the                     } the                 } +             } -             returnmath.min (min, min2); the}Else {Bayi             returnmin; the         } the     } -      -      Public Static DoubleDistance (Point point1,point point2) { the         returnMath.sqrt ((Point1.getx ()-POINT2.GETX ()) * (Point1.getx ()-point2.getx ()) the+ (Point1.gety ()-point2.gety ()) * (Point1.gety ()-point2.gety ())); the     } the } -  the classPointImplementsComparable<point> { the     //x.y coordinates of the point the     Private Doublex;94     Private Doubley; the  the      Public voidSetX (Doublex) { the          This. x =x;98     } About  -      Public voidSety (Doubley) {101          This. y =y;102     }103 104      Public DoubleGetX () { the         returnx;106     }107 108      Public DoubleGetY () {109         returny; the     }111  the @Override113      PublicString toString () { the         return"(" +x+ "," +y+ ")"; the     } the @Override117      Public intcompareTo (Point O) {118         if(X >o.getx ())119             return1; -         Else if(x = =O.getx ()) {121             return0;122}Else {123             return-1;124         } the     }126}

Java Learning (10): Finding the closest point to the problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.