您的位置:首页 > 其它

hihocoder 1040 矩形判断解题报告

2016-05-14 21:44 281 查看
    用来热身的一道题目,根据题意输入8个点表示4条线,可以利用4个向量来表示这四条线,判断是否组成矩形只需拿出任一条向量判断其余3条向量是否有两条垂直向量,两条平行向量。如果有,则构成矩形,没有则输出"NO"

代码如下。

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int num = 4;
struct Data
{
int x,y;
};
struct line
{
Data one,two;
line(int a = 0,int b = 0,int c = 0,int d = 0)
{
one.x = a;one.y = b;
two.x = c;two.y = d;
}
}T[num];

void input()
{
int a,b,c,d;
for(int i = 0; i < 4; ++i)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
T[i] = *new struct line(a,b,c,d);
}
}
bool check()
{
int p = 0,cu = 0;
int a = T[0].one.x,b = T[0].one.y,c = T[0].two.x,d = T[0].two.y;
int a2,b2,c2,d2;
for(int i = 1; i < 4;++i)
{
a2 = T[i].one.x;
b2 = T[i].one.y;
c2 = T[i].two.x;
d2 = T[i].two.y;
int x1 = c - a; int y1 = d - b;
int x2 = c2 - a2; int y2 = d2 - b2;
if(x1 * x2 + y1 * y2 == 0)
cu++;
if(x2 * y1 - y2 * x1 == 0)
p++;
}
if(cu == 2 && p == 1)
return true;
else return false;
}
int main()
{
int n;cin>>n;
while(n--)
{
input();
if(check())
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: