一批点中,找出能找出多少对可以组成的矩形

/*

思考题:

有一个Point类,保存了x,y.

通过控制台录入了n,

然后录入了n个坐标。

保存在了集合中。

排序 去重

查找这些点能组成矩形的情况,有多少组。

排序,则需要什么规则

去重,需要如何处理Point类中的方法

即排序又去重,应当采用什么类

* */

java 复制代码
package 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);
    }
}
相关推荐
阿白的白日梦20 小时前
winget基础管理---更新/修改源为国内源
windows
埃博拉酱5 天前
VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传
windows·ssh·visual studio code
唐宋元明清21885 天前
.NET 本地Db数据库-技术方案选型
windows·c#
加号35 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
tryCbest5 天前
Windows环境下配置pip镜像源
windows·pip
呉師傅5 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑
百事牛科技6 天前
保护文档安全:PDF限制功能详解与实操
windows·pdf
一个人旅程~6 天前
如何用命令行把win10/win11设置为长期暂停更新?
linux·windows·经验分享·电脑
一个假的前端男6 天前
[特殊字符] Flutter 安装完整指南 Windows—— 2026最新版
windows·flutter
倚肆6 天前
在 Windows Docker 中安装并配置 Nginx (映射 Windows 端口与路径)
windows·nginx·docker