UVA_11800_DetermineTheShape
来源:互联网 发布:智云软件开发 编辑:程序博客网 时间:2024/06/03 21:18
11800 - Determine the Shape
Time limit: 1.000 seconds
A toy company recently found that toys like revolver,
machine guns, ghting planes are making children vio-
lent and destroying the peace of the world. The parents
also began to avoid these toys and inclined to educa-
tional toys. So they decided to manufacture educa-
tional toys. One of these is a electric touch pad on
which children can put four points and the program will
automatically join the points to form a closed shape.
Children will try to guess the shape and when they
press a button then it will automatically announce the
shape. But they are struggling to determine the shape
and seek your help.
Your task is simple. You are given four points, no
three of them are collinear, you have to output the
simple polygonal shape formed by these points in the
following order:
Square
Rectangle
Rhombus
Parallelogram
Trapezium
Ordinary Quadrilateral
For example if it is possible to form a square with the four points you must output `Square', if it is
not possible to form a square but possible to form a rectangle you must output `Rectangle' and so on.
Input
Input starts with an integer T, the number of test cases (T<50000). Each test case contains 4 lines.
Each of the lines contains two space separated integers xi yi (-10000<xi; yi<10000) which are the
coordinate values of a point.
Output
For each set of input output one line in the format `Case k: s'. Here k is the case number starting
from 1 and s is the shape as described above. See sample input output for more details.
Note: If you have forgotten elementary geometry, here is the de nitions to remind you:
Square: All sides are of equal size all angles are 90◦
Rectangle: Opposite sides are of equal size and all angles are 90◦
Rhombus: All sides are of equal size but no angle is 90◦
Parallelogram: Opposite sides are of equal size but no angle is 90◦
Trapezium: Any two opposite sides are parallel but the other two is not.
Simple Polygon: Polygon having no self intersecting edge.
Sample Input
6
0 0
2 0
2 2
0 2
0 0
3 0
3 2
0 2
0 0
8 4
5 0
3 4
0 0
2 0
3 2
1 2
0 0
5 0
4 3
1 3
0 0
5 0
4 3
1 4
Sample Output
Case 1: Square
Case 2: Rectangle
Case 3: Rhombus
Case 4: Parallelogram
Case 5: Trapezium
Case 6: Ordinary Quadrilateral
题目意思判断一个四边形是哪种四边形
正方形 长方形 菱形 平行四边形 梯形 普通四边形
注意的就是 四边形的点并不是按照顺序给的
因此需要处理下
#include <iostream>#include <cstdio>#include <cmath> //需要用到部分函数using namespace std;const double PI=acos(-1.0); //π的大小const double eps=1e-8; //允许误差int dcmp(double x) //误差修正{ if(fabs(x)<eps)return 0; if(x>0) return 1; return -1;}inline double sqr(double x) //平方{ return (x*x);}inline double deg2rad(double deg) //角度制换算弧度制{ return (deg*PI/180.0);}inline double rad2deg(double rad) //弧度制换算角度制{ return (rad*180.0/PI);}//以上基础//以下点类struct Point //点类,向量类{ double x,y; Point(){} Point(double a,double b):x(a),y(b){} void input() { scanf("%lf%lf",&x,&y); } friend Point operator +(const Point &a,const Point &b) //点之和 { return Point(a.x+b.x,a.y+b.y); } friend Point operator -(const Point &a,const Point &b) //点之差 { return Point(a.x-b.x,a.y-b.y); } friend bool operator ==(const Point &a,const Point &b) //点相同 { return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0; } friend Point operator *(const Point &a,const double &b) //数乘 { return Point(a.x*b,a.y*b); } friend Point operator *(const double &a,const Point &b) { return Point(a*b.x,a*b.y); } friend Point operator /(const Point &a,const double &b) //除以某数 { return Point(a.x/b,a.y/b); } double norm() //向量的模长 { return sqrt(sqr(x)+sqr(y)); } double polarangle() //向量的极角 { return atan2(y,x); } void rotate_point(double A) //P点以原点为中心逆时针旋转A弧度 { double tx=x,ty=y; x=tx*cos(A)-ty*sin(A); y=tx*sin(A)+ty*cos(A); }};typedef Point Vector; //向量是点的别名double det(const Point &a,const Point &b) //向量a,b的叉积{ //a转向b的有向面积(逆时针转为正) return a.x*b.y-a.y*b.x;}double dot(const Point &a,const Point &b) //向量a,b的点积{ return a.x*b.x+a.y*b.y;}double dist(const Point &a,const Point &b) //点a与点b间距离{ return (a-b).norm();}Point rotate_point(const Point &p,double A) //P点以原点为中心逆时针旋转A弧度{ double tx=p.x,ty=p.y; return Point(tx*cos(A)-ty*sin(A),tx*sin(A)+ty*cos(A));}double angle(Vector a,Vector b) //a与b向量之间的夹角(无向){ return acos(dot(a,b)/a.norm()/b.norm());}Vector normal_vector(Vector a) //向量的单位法向量{ //需保证向量不是0向量 double l=a.norm(); return Vector(-a.y/l,a.x/l);}// 叉乘cross product of (o->a) and (o->b)double cross(const Point &a,const Point &b, const Point &o){ return (a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);}//以上点模板//以下直线及线段模板struct Segment //线段类{ Point a,b; Segment(){} Segment(Point x,Point y):a(x),b(y){} Segment(double x1,double y1,double x2,double y2) { a.x=x1;a.y=y1;b.x=x2;b.y=y2; } double len() { return fabs((a-b).norm()); }};typedef Segment Sline; //同时也是直线的两点表示法Segment point_make_segment(const Point &a,const Point &b) //点造线段{ return Segment(a,b);}bool parallel(const Sline &a,const Sline &b) //线段所在直线是否平行{ return !dcmp(det(a.a-a.b,b.a-b.b));}//以上线段类int judge(Segment a,Segment b,Segment c,Segment d){ int ans=0; if(parallel(a,c)||parallel(b,d)) { if(a.len()!=c.len()||b.len()!=d.len()) //两边相等两边不等 { ans=max(ans,2); } else { if(a.len()==b.len()) { if(!dot(a.a-a.b,b.a-b.b)) ans=max(ans,6); else ans=max(ans,4);; } else { if(!dot(a.a-a.b,b.a-b.b)) ans=max(ans,5); else ans=max(ans,3); } } } else ans=max(ans,1); return ans;}int main(){ int t; Point A,B,C,D; Segment a,b,c,d; scanf("%d",&t); int ans; for(int ca=1;ca<=t;ca++) { ans=0; A.input();B.input();C.input();D.input(); a=Segment(A,B);b=Segment(B,C);c=Segment(C,D);d=Segment(D,A); ans=max(ans,judge(a,b,c,d)); a=Segment(A,B);b=Segment(A,C);c=Segment(C,D);d=Segment(D,B); ans=max(ans,judge(a,b,c,d)); a=Segment(A,D);b=Segment(A,C);c=Segment(B,C);d=Segment(D,B); ans=max(ans,judge(a,b,c,d)); printf("Case %d: ",ca); switch(ans) { case 1:printf("Ordinary Quadrilateral\n");break; case 2:printf("Trapezium\n");break; case 3:printf("Parallelogram\n");break; case 4:printf("Rhombus\n");break; case 5:printf("Rectangle\n");break; case 6:printf("Square\n");break; } } return 0;}
- UVA_11800_DetermineTheShape
- 计步器判断运动状态2
- leetcode Remove Duplicates from Sorted Array II
- NYOJ 37 回文字符串
- codeblocks的c99设置
- 关于嵌入式系统的学习路线图
- UVA_11800_DetermineTheShape
- block循环引用的理解和self.navigationController的理解
- list_entry(ptr, type, member)
- POJ 3437 Tree Grafting(二叉树高度)
- 面试问题
- Spark启动错误Exception in thread “main” java.lang.NoSuchMethodError: scala.collection.immutable.HashSet$
- apue第三版 在信号处理程序中调用不可再入函数 的例子遇到的问题
- [53]Maximum Subarray
- 实时操作系统