程序要实现的目的就是输入n个点,然后按照他们的x 坐标进行排序。
import java.util.*; import java.math.*; class Point { public float x; public float y; //构造方法 public Point(float a,float b) { x=a; y=b; } double get_dis(Point a, Point b) { return Math.sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } } // 按照x排序升序 class MyComparatorx implements Comparator<Point> { public int compare(Point o1, Point o2) { if (o1.x < o2.x) { return -1; } else if (o1.x > o2.x) { return 1; } else { return 0; } } } // 按照y排序升序 class MyComparatory implements Comparator<Point> { public int compare(Point o1, Point o2) { if (o1.y < o2.y) { return -1; } else if (o1.y > o2.y) { return 1; } else { return 0; } } } // ---------主函数---------// public class ACM1007 { public static void main(String[] args) { Point[] coordinate = new Point[100005]; Point[] coordinate_x = new Point[100005]; Point[] coordinate_y = new Point[100005]; Scanner reader = new Scanner(System.in); int i, n; while (reader.hasNextInt()) { n = reader.nextInt(); if (n == 0) break; for (i = 0; i < n; i++) { float point_x=reader.nextFloat(); float point_y=reader.nextFloat(); coordinate[i]=new Point(point_x,point_y); //coordinate_x[i]=coordinate[i]; } MyComparatorx cmp_x=new MyComparatorx(); Arrays.sort(coordinate, cmp_x); for(int j=0;j<n;j++){ System.out.println(coordinate[i].x); } } } }
winterfelll
慕的地6079101
yangzhao
Tobey_滔
ziom
相关分类