Intersecting Lines ACM PKU http://acm.pku.cn/JudgeOnline/problem?id=1269

自己的问题自己解决了,凡事还得靠自己啊!
#include <iostream>
using namespace std;
int main ()
{
int n;
cin >>n;
double x1,x2,x3,x4,y1,y2,y3,y4;
double a[10][8];
for (int i=0;i<n;i++)
   for (int k=0;k<8;k++)
{
    cin>>a[i][k];

}
cout<<"INTERSECTING LINES OUTPUT"<<endl;
for (int m=0;m<n;m++)
{
   x1=a[m][0];y1=a[m][1];
   x2=a[m][2];y2=a[m][3];
   x3=a[m][4];y3=a[m][5];
   x4=a[m][6];y4=a[m][7];
   double X,Y;
   if (x1==x2&&x3!=x4)
   {
    X=x1;
    Y=((y4-y3)/(x4-x3))*(X-x3)+y3;
   }
   else if (x3==x4&&x1!=x2)
   {
    X=x3;
    Y=((y2-y1)/(x2-x1))*(X-x1)+y1;
   }
   else if((x1==x2)&&(x3==x4))

//之前我把这两种情况合在一起写的却怎么交也交不上,后来分开写才知道里面有情况没有考虑到啊!
   {
    if (x2==x3)
    {
           cout<<"LINE"<<endl;
            continue;
    }
    else 
    { 
     cout<<"NONE"<<endl;
            continue;            
    }

   }
   else if (((x1!=x2)&&(x3!=x4))&&(y4-y3)/(x4-x3)==(y2-y1)/(x2-x1))
   {
    if ((y4-y1)/(x4-x1)==(y3-y2)/(x3-x2))
    {
           cout<<"LINE"<<endl;
            continue;
    }
    else 
    { 
     cout<<"NONE"<<endl;
            continue;            
    }
   } 
   else if ((y4-y3)/(x4-x3)!=(y2-y1)/(x2-x1)) 
   {
    X=(y3-y1-((y4-y3)/(x4-x3))*x3+((y2-y1)/(x2-x1))*x1)/(((y2-y1)/(x2-x1))-((y4-y3)/(x4-x3)));
    Y=(((y4-y3)/(x4-x3))*y1-((y2-y1)/(x2-x1))*y3+((y2-y1)/(x2-x1))*((y4-y3)/(x4-x3))*x3-((y4-y3)/(x4-x3))*((y2-y1)/(x2-x1))*x1)/(((y4-y3)/(x4-x3))-((y2-y1)/(x2-x1)));
   }
    cout<<"POINT ";
    printf("%.2lf",X);
    cout<<" ";
    printf("%.2lf\n",Y);
} 
cout<<"END OF OUTPUT"<<endl;
return 0;
}

posted on 2011-05-06 19:40  _Clarence  阅读(106)  评论(0编辑  收藏  举报

导航