Square | ||||||
| ||||||
Description | ||||||
You are given four points, determine whether they can form a square in a two-dimensional plane. Note the area of the square must be positive. | ||||||
Input | ||||||
There are multiple test cases. Each test case is a single line containing 8 integers x1 y1 x2 y2 x3 y3 x4 y4, which are the coordinates of the four points. All integers in the input are in the range of [−109 , 109 ]. | ||||||
Output | ||||||
For each test case, output a single line containing “YES” or “NO”. | ||||||
Sample Input | ||||||
0 0 0 1 1 1 1 0 | ||||||
Sample Output | ||||||
YES | ||||||
Source | ||||||
“尚学堂杯”2015级程序设计竞赛(11月)正式赛 |
给你八个点,判断它能否在直角坐标系中构建成一个正方形,如果可以,输出YES,否则输出NO.
思路:求出正方形中的六条边(四个边,两个对角线)的长度,然后对其排序,前四个边长相同并且后两个相同,并且前边边长的平方*2==后边边长的平方。
注意的点:
1、数据比较大,当有平方加和之类的操作的时候是一定会超int的。
2、注意重点现象。
AC代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define ll long long int
struct zuobiao
{
ll x,y;
}p[5];
int main()
{
while(~scanf("%lld%lld%lld%lld%lld%lld%lld%lld",&p[0].x,&p[0].y,&p[1].x,&p[1].y,&p[2].x,&p[2].y,&p[3].x,&p[3].y))
{
ll juli[6];
int cont=0;
for(int i=0;i<4;i++)
{
for(int j=0;j<i;j++)
{
juli[cont]=(p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y);
cont++;
}
}
sort(juli,juli+cont);
if(juli[0]==juli[1]&&juli[1]==juli[2]&&juli[2]==juli[3]&&juli[4]==juli[5]&&juli[0]*2==juli[4]&&juli[0]!=0)
{
printf("YES\n");
}
else printf("NO\n");
}
}