java编程:输入n个二维平面上的坐标点,所有的坐标点按照以x轴数为第一关键字,y轴为第二关键字顺序从小到大排序,输入说明:输入一个点的个数n个,然后输入n个二维整数坐标值。
import java.util.Arrays; import java.util.Scanner; public class Demo { public static void main(String args[]) { Scanner in = new Scanner(System.in); int n = in.nextInt(); MyPoint[] points = new MyPoint[n]; for (int i = 0; i < n; i++) { points[i] = new MyPoint(in.nextInt(), in.nextInt()); } Arrays.sort(points); //System.out.println("排序后:"); for (int i = 0; i < n; i++) { System.out.println(points[i].x + " " + points[i].y); } in.close(); } } class MyPoint implements Comparable<MyPoint> { public int x; public int y; public MyPoint(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(MyPoint other) { if (this.x > other.x) { return 1; } else if (this.x < other.x) { return -1; } else if (this.y > other.y) { return 1; } else if (this.y < other.y) { return -1; } return 0; } }
java编程:输入n个二维平面上的坐标点,所有的坐标点按照以x轴数为第一关键字,y轴为第二关键字顺序从小到大排序
最新推荐文章于 2024-03-09 21:26:27 发布
本文介绍了一个Java程序,该程序接收用户输入的多个二维坐标点,并按照x轴和y轴数值进行排序。首先读取点的数量,然后逐一读取每个点的坐标,使用自定义的MyPoint类存储坐标并实现Comparable接口以支持排序。最后,利用Arrays.sort方法对坐标点进行排序,并输出排序后的结果。


506

被折叠的 条评论
为什么被折叠?



