/*
思考题:
有一个Point类,保存了x,y.
通过控制台录入了n,
然后录入了n个坐标。
保存在了集合中。
排序 去重
查找这些点能组成矩形的情况,有多少组。
排序,则需要什么规则
去重,需要如何处理Point类中的方法
即排序又去重,应当采用什么类
* */
javapackage point; import java.util.*; class Point implements Comparable<Point>{ private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Point point = (Point) o; return x == point.x && y == point.y; } @Override public int hashCode() { return Objects.hash(x, y); } @Override public String toString() { return "Point{" + "x=" + x + ", y=" + y + '}'; } @Override public int compareTo(Point o) { if(this.x==o.x) return this.y-o.y; else return this.x-o.x; } } public class MainTest { public static void main(String[] args) { Scanner in= new Scanner(System.in); TreeSet<Point> set= new TreeSet<>(); int n=in.nextInt(); for(int i=0;i<n;i++){ int x,y; x=in.nextInt(); y=in.nextInt(); set.add(new Point(x,y)); } int res=0; ArrayList<Point> list = new ArrayList<>(set);//set转为列表 for(int i=0;i<list.size();i++){ for(int j=0;j<list.size();j++){ if(list.get(i).getX()<list.get(j).getX() && list.get(i).getY()<list.get(j).getY() &&list.contains(new Point(list.get(i).getX(),list.get(j).getY())) &&list.contains(new Point( list.get(j).getY(),list.get(i).getX())) ){ res++; } } } System.out.println(res); } }
一批点中,找出能找出多少对可以组成的矩形
laocooon5238578862024-09-27 23:47
相关推荐
陈苏同学9 小时前
[已解决] VS Code / Cursor / Trae 的 PowerShell 终端 conda activate 进不去环境的常见问题辰%10 小时前
如何重启pycharm中的项目?iangyu12 小时前
【windows server脚本每天从网络盘复制到本地】love530love14 小时前
家用或办公 Windows 电脑玩人工智能开源项目配备核显的必要性(含 NPU 及显卡类型补充)周胡杰1 天前
鸿蒙接入flutter环境变量配置windows-命令行或者手动配置-到项目的创建-运行demo项目几道之旅1 天前
分别在windows和linux上使用curl,有啥区别?一直奔跑在路上1 天前
【Ansible】基于windows主机,采用NTLM+HTTPS 认证部署郭逍遥1 天前
[工具]B站缓存工具箱 (By 郭逍遥)x-cmd1 天前
[250512] Node.js 24 发布:ClangCL 构建,升级 V8 引擎、集成 npm 11IT空门:门主1 天前
本地的ip实现https访问-OpenSSL安装+ssl正式的生成(Windows 系统)