NYOJ 1099 Lan Xiang's Square(给出四点判断是否能构成正方形)

来源:互联网 发布:渡口网络破产 编辑:程序博客网 时间:2024/06/05 19:23


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
样例输入
11 1-1 1-1 -11 -1
样例输出
Yes
提示
you think this is a easy problem ? you dare submit, i promise you get a WA. :)
来源
myself
上传者
ACM_张开创

对于给的四个点首先应该先进行排序,排序时自己一定要清楚排的顺序,不然会影响后面的计算的。然后就是分别把四条边以及两条对角线的长度给求出来。下面开始对边进行判断,四条边都相等且两条对角线相等(当然长度不会为0,这个要考虑到)的图形可以构成正方形。

代码:

#include<stdio.h>#include<algorithm>#include<math.h>#include<string.h>using namespace std;struct A{double x,y;}a[4];bool cmp (A a,A b){if(a.x != b.x)return a.x < b.x;elsereturn a.y > b.y; }int main(){int t;scanf("%d",&t);while(t--){for(int i = 0;i < 4;i++)scanf("%lf%lf",&a[i].x,&a[i].y);sort(a,a + 4,cmp);//四个点先排序 //for(int i = 0;i < 4;i++)//printf(" ##  %lf   %lf\n",a[i].x,a[i].y);double l1,l2,l3,l4,l5,l6; //l1~l4为四条边,l5.l6为两条对角线的长度 l1 = sqrt(pow((a[0].x - a[1].x),2) + pow((a[0].y - a[1].y),2));l2 = sqrt(pow((a[0].x - a[2].x),2) + pow((a[0].y - a[2].y),2));l3 = sqrt(pow((a[2].x - a[3].x),2) + pow((a[2].y - a[3].y),2));l4 = sqrt(pow((a[1].x - a[3].x),2) + pow((a[1].y - a[3].y),2));l5 = sqrt(pow((a[0].x - a[3].x),2) + pow((a[0].y - a[3].y),2));l6 = sqrt(pow((a[1].x - a[2].x),2) + pow((a[1].y - a[2].y),2));if(l5 != l6 || l5 == 0) //两对角线之间的判断 {printf("No\n");}else if(l5 == l6)  //对角线相等的情况下判断4条边 {if(l1 == l2 && l1 == l3 && l1 == l4)printf("Yes\n");elseprintf("No\n");}}return 0;}

0 0