标签:
The king found his adherents were building four sanctuaries for him. He is interested about the positions of the sanctuaries and wants to know whether they would form a parallelogram, rectangle, diamond, square or anything else.
The first line of the input is
For every test case, you should output Case
#t: first, where
| Sample Input | Sample Output |
|---|---|
5 0 0 1 1 2 1 1 0 0 0 0 1 2 1 2 0 0 0 2 1 4 0 2 -1 0 0 0 1 1 1 1 0 0 0 1 1 2 1 3 0 |
Case #1: Parallelogram Case #2: Rectangle Case #3: Diamond Case #4: Square Case #5: Others |
Sichuan State Programming Contest 2012
原题链接:http://acm.uestc.edu.cn/#/problem/show/93
题意:按顺时针(clockwise)方向给你四个点的坐标,判断它是否是平行四边形,矩形,菱形,正方形。
几何,肯定是用向量,具体判断四边形的形状,就看你初高中的几何了,具体判断,看下面这张图。
AC代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cmath>
using namespace std;
struct point
{
int x,y;
} p[4];
int main()
{
int T;
int kase=0;
cin>>T;
while(T--)
{
for(int i=0; i<4; i++)
{
cin>>p[i].x>>p[i].y;
}
int px= (p[1].x-p[0].x);
int py= (p[1].y-p[0].y);
int qx= (p[2].x-p[3].x);
int qy= (p[2].y-p[3].y);
int rx= (p[2].x-p[1].x);
int ry=(p[2].y-p[1].y);
int tx=p[3].x-p[0].x;
int ty=p[3].y-p[0].y;
printf("Case #%d: ",++kase);
if(px==qx&&py==qy&&rx==tx&&ry==ty)//两组对边分别相等-->平行四边形
{
if(px*rx+py*ry==0)//+邻边垂直-->矩形或正方形
{
if(px*px+py*py==rx*rx+ry*ry)//+邻边相等-->正方形
{
cout<<"Square"<<endl;
}
else
cout<<"Rectangle"<<endl;
}
else if(px*px+py*py==rx*rx+ry*ry)//+邻边不垂直但相等-->菱形
{
cout<<"Diamond"<<endl;
}
else//平行四边形
cout<<"Parallelogram"<<endl;
}
else
cout<<"Others"<<endl;
}
return 0;
}
CDOJ 93 King's Sanctuary【判断四边形形状】
标签:
原文地址:http://blog.csdn.net/hurmishine/article/details/52083635