| ||||||||||
Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
16天终于A下了50道~在此留下牙齿印,以示向100道冲刺的决心;接下来一个月可能要回家,再也没电脑上网了,我会永远想念这一段A题的日子#include <iostream> #include <iomanip> using namespace std; struct Point { int x; int y; }; // 这道题关键在于处理直线和X轴垂直的情况,因为这种情况斜率是无穷大的,无法通过常规处理 // 其它的情况自己可以通过斜率和截距推导出结果 int main() { int n; int i; double k12,b12; // 直线1的斜率和Y截距 double k34,b34; // 直线2的斜率和Y截距 Point p[4]; double intersectX, intersectY; // 相交点 cin>>n; cout<<"INTERSECTING LINES OUTPUT"<<endl; while (n--) { // p[1]和P[2]为直线1的两个点,p[3]和p[4]为直线2的两个点 for (i=1; i<=4; i++) { cin>>p[i].x>>p[i].y; } if (p[1].x==p[2].x && p[3].x==p[4].x) // 直线1和直线2都与X轴垂直 { if (p[2].x == p[3].x) { cout<<"LINE"<<endl; } else { cout<<"NONE"<<endl; } } else if (p[1].x==p[2].x && p[3].x!=p[4].x) // 直线1垂直,直线2不垂直 { k34 = double(p[4].y - p[3].y)/(p[4].x-p[3].x); b34 = double(p[3].x*p[4].y - p[4].x*p[3].y)/(p[3].x-p[4].x); intersectX = p[1].x; intersectY = k34*intersectX + b34; cout<<setiosflags(ios::fixed)<<setprecision(2)<<"POINT "<<intersectX<<" "<<intersectY<<endl; } else if (p[1].x!=p[2].x && p[3].x==p[4].x)// 直线1不垂直,直线2垂直 { k12 = double(p[2].y - p[1].y)/(p[2].x-p[1].x); b12 = double(p[1].x*p[2].y - p[2].x*p[1].y)/(p[1].x-p[2].x); intersectX = p[3].x; intersectY = k12*intersectX + b12; cout<<setiosflags(ios::fixed)<<setprecision(2)<<"POINT "<<intersectX<<" "<<intersectY<<endl; } else // 都不垂直 { k12 = double(p[2].y - p[1].y)/(p[2].x-p[1].x); b12 = double(p[1].x*p[2].y - p[2].x*p[1].y)/(p[1].x-p[2].x); k34 = double(p[4].y - p[3].y)/(p[4].x-p[3].x); b34 = double(p[3].x*p[4].y - p[4].x*p[3].y)/(p[3].x-p[4].x); if (k12 == k34) // 斜率相同 { if (k12*p[1].x+b12 == k34*p[1].x+b34) // 重叠 { cout<<"LINE"<<endl; } else // 平行 { cout<<"NONE"<<endl; } } else // 斜率不同 { intersectX = -(b34-b12)/(k34-k12); // 相交 intersectY = k12*intersectX + b12; cout<<setiosflags(ios::fixed)<<setprecision(2)<<"POINT "<<intersectX<<" "<<intersectY<<endl; } } } cout<<"END OF OUTPUT"<<endl; return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator