nyoj 1099 Lan Xiang's Square (给出四个点的坐标,检测能否组成正方形)

实验室小学妹这次腾讯模拟笔试中遇到相似的题,于是找了一道,拿来做一下。。。。

Lan Xiang’s Square
时间限制:1000 ms | 内存限制:65535 KB
难度:0

描述

Excavator technology which is strong, fast to Shandong to find Lan Xiang.

Then the question comes.. :)

for this problem , i will give you four points. you just judge if they can form a square.
if they can, print “Yes”, else print “No”.
Easy ? just AC it.

输入
T <= 105 cases.
for every case
four points, and every point is a grid point .-10^8 <= all interger <= 10^8。
grid point is both x and y are interger.
输出
Yes or No
样例输入

1
1 1
-1 1
-1 -1
1 -1

样例输出

Yes

提示
you think this is a easy problem ? you dare submit, i promise you get a WA. :)

给出四个点的坐标,检测能否组成正方形

思路:先排序,再证明四个边相等,再证明两个对角线相等。。关键点,一定要先排序,先排序,先排序。。。。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Point{
    double x,y;
    Point(double _x, double _y):x(_x),y(_y){};
    bool operator < (const Point &a) const{
        return x==a.x?y<a.y:x<a.x;
    }
};
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        vector<Point> a;
        double xx, yy;
        for (int i = 0; i < 4; ++i) {
            cin>>xx>>yy;
            a.push_back(Point(xx, yy));
        }
        sort(a.begin(), a.end());
        double l1, l2, l3, l4, l5, l6;
        l1=(a[0].x-a[2].x)*(a[0].x-a[2].x)+(a[0].y-a[2].y)*(a[0].y-a[2].y);
        l2=(a[0].x-a[1].x)*(a[0].x-a[1].x)+(a[0].y-a[1].y)*(a[0].y-a[1].y);
        l3=(a[3].x-a[1].x)*(a[3].x-a[1].x)+(a[3].y-a[1].y)*(a[3].y-a[1].y);
        l4=(a[2].x-a[3].x)*(a[2].x-a[3].x)+(a[2].y-a[3].y)*(a[2].y-a[3].y);
        l5 = (a[1].x-a[2].x)*(a[1].x-a[2].x)+(a[1].y-a[2].y)*(a[1].y-a[2].y);
        l6 = (a[0].x-a[3].x)*(a[0].x-a[3].x)+(a[0].y-a[3].y)*(a[0].y-a[3].y);
        if(l1==l2&&l3==l4&&l1==l3&&l1!=0&&l5==l6)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
}

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值