正方形poj

总时间限制: 

3500ms

内存限制: 

65536kB

描述

给定直角坐标系中的若干整点,请寻找可以由这些点组成的正方形,并统计它们的个数。

输入

包括多组数据,每组数据的第一行是整点的个数n(1<=n<=1000),其后n行每行由两个整数组成,表示一个点的x、y坐标。输入保证一组数据中不会出现相同的点,且坐标的绝对值小于等于20000。输入以一组n=0的数据结尾。

输出

对于每组输入数据,输出一个数,表示这组数据中的点可以组成的正方形的数量。

样例输入

4
1 0
0 1
1 1
0 0
9
0 0
1 0
2 0
0 2
1 2
2 2
0 1
1 1
2 1
4
-2 5
3 7
0 0
5 2
0

样例输出

1
6
1

示例代码

#include <iostream>
#include <map>
#include <cmath>
using namespace std;

int main() {
    int n;
    while (cin >> n && n != 0) {
        map<pair<int, int>, int> points;
        for (int i = 0; i < n; i++) {
            int x, y;
            cin >> x >> y;
            points[{x, y}] = 1;
        }

        int count = 0;
        for (auto it1 = points.begin(); it1 != points.end(); it1++) {
            for (auto it2 = next(it1); it2 != points.end(); it2++) {
                int dx = it2->first.first - it1->first.first;
                int dy = it2->first.second - it1->first.second;
                if (points.count({it1->first.first + dy, it1->first.second - dx}) 
                    && points.count({it2->first.first + dy, it2->first.second - dx})) {
                    count++;
                }
            }
        }

        cout << count / 2 << endl;
    }

    return 0;
}

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值