2013 - ECJTU 暑期训练赛第七场-problem-A
来源:互联网 发布:jre 7u4 windows i586 编辑:程序博客网 时间:2024/06/10 18:07
A -O.O
Crawling in process...Crawling failedTime Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64uDescription
We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line because they are on top of one another (i.e. they are the same line), 3) intersect in a point. In this problem you will use your algebraic knowledge to create a program that determines how and where two lines intersect.
Your program will repeatedly read in four points that define two lines in the x-y plane and determine how and where the lines intersect. All numbers required by this problem will be reasonable, say between -1000 and 1000.
Your program will repeatedly read in four points that define two lines in the x-y plane and determine how and where the lines intersect. All numbers required by this problem will be reasonable, say between -1000 and 1000.
Input
The first line contains an integer N between 1 and 10 describing how many pairs of lines are represented. The next N lines will each contain eight integers. These integers represent the coordinates of four points on the plane in the order x1y1x2y2x3y3x4y4. Thus each of these input lines represents two lines on the plane: the line through (x1,y1) and (x2,y2) and the line through (x3,y3) and (x4,y4). The point (x1,y1) is always distinct from (x2,y2). Likewise with (x3,y3) and (x4,y4).
Output
There should be N+2 lines of output. The first line of output should read INTERSECTING LINES OUTPUT. There will then be one line of output for each pair of planar lines represented by a line of input, describing how the lines intersect: none, line, or point. If the intersection is a point then your program should output the x and y coordinates of the point, correct to two decimal places. The final line of output should read "END OF OUTPUT".
Sample Input
50 0 4 4 0 4 4 05 0 7 6 1 0 2 35 0 7 6 3 -6 4 -32 0 2 27 1 5 18 50 3 4 0 1 2 2 5
Sample Output
INTERSECTING LINES OUTPUTPOINT 2.00 2.00NONELINEPOINT 2.00 5.00POINT 1.07 2.20END OF OUTPUT
AC代码+解释:
#include<iostream>#include<cstdio>#include<iomanip>using namespace std;double x1,x2,x3,x4,y1,y2,y3,y4,k1,k2,b1,b2,x,y;int main(){ int n,i; while(cin>>n) { cout<<"INTERSECTING LINES OUTPUT"<<endl; for(i=0; i<n; i++) { cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4; if(x1!=x2&&x3!=x4) { k1=(y1-y2)/(x1-x2);//直线A->B的斜率 k2=(y3-y4)/(x3-x4);//直线C->D的斜率 b1=y1-(k1*x1);//直线A->B的b b2=y3-(k2*x3);//直线C->D的b if(k1==k2)//如果相等,表明不是平行就是重合 { if((x3*k1+b1)==y3)//如果直线C->D上点在直线A->B上表明两条直线重合了 cout<<"LINE"<<endl; else//否则平行 cout<<"NONE"<<endl; } if(k1!=k2)//如果斜率不相等,表明斜率一定存在 { x=(b2-b1)/(k1-k2); y=(k1*x)+b1; cout<<"POINT "; printf("%.2lf %.2lf\n",x,y); } } if(x1==x2||x3==x4)//考虑两条直线的斜率都不存在的情况 { if(x1==x2&&x3==x4)//如果直线A->B和C->D的两个点横坐标相等 { if(x1==x3)//如果四个点都在同一条直线上 cout<<"LINE"<<endl; else//否则就平行 cout<<"NONE"<<endl; } if(x1==x2&&x3!=x4)//下面两种情况是如果有一条直线斜率不存在而另一条直线存在 { k2=(y3-y4)/(x3-x4); b2=y3-k2*x3; y1=k2*x1+b2; cout<<"POINT "; printf("%.2lf %.2lf\n",x1,y1); } if(x1!=x2&&x3==x4) { k1=(y1-y2)/(x1-x2); b1=y1-k1*x1; y3=k1*x3+b1; cout<<"POINT "; printf("%.2lf %.2lf\n",x3,y3); } } } cout<<"END OF OUTPUT"<<endl; } return 0;}
- 2013 - ECJTU 暑期训练赛第七场-problem-A
- 2013 - ECJTU 暑期训练赛第七场-problem-F
- 2013 - ECJTU 暑期训练赛第七场-problem-G
- 2013 - ECJTU 暑期训练赛第七场-problem-H
- 2013 - ECJTU 暑期训练赛第七场-problem-G
- 2013 - ECJTU 暑期训练赛第二场-problem-A
- 2013 - ECJTU 暑期训练赛第四场-problem-A
- 2013 - ECJTU 暑期训练赛第五场-problem-A
- 2013 - ECJTU 暑期训练赛第八场-problem-A
- 2013 - ECJTU 暑期训练赛第二场-problem-E
- 2013 - ECJTU 暑期训练赛第二场-problem-G
- 2013 - ECJTU 暑期训练赛第二场-problem-H
- 2013 - ECJTU 暑期训练赛第二场-problem-J
- 2013 - ECJTU 暑期训练赛第三场-problem-B
- 2013 - ECJTU 暑期训练赛第三场-problem-E
- 2013 - ECJTU 暑期训练赛第三场-problem-D
- 2013 - ECJTU 暑期训练赛第三场-problem-L
- 2013 - ECJTU 暑期训练赛第三场-problem-H
- ORACLE日期时间函数大全
- 关于AlertDialog"全局对话框"
- Android中Preference的使用以及监听事件分析
- 在ios 下获取 rootviewcontroller 的版本不同的问题
- dwqdwqdwqdwq
- 2013 - ECJTU 暑期训练赛第七场-problem-A
- php上传时,上传文件类型对照表
- POJ 2441 Arrange the Bulls
- 引用函数和非引用函数
- android kernel和标准linux kernel的区别
- web.xml里面定义在servletcontex、Servlet和Filter的参数:
- Linux设备驱动之内存映射
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- 请不要辜负这个时代