POJ 1118 Lining Up 叉积,三点共线。

题意:给定N个坐标点, 现在要求出在共线的点的个数,输出最大值

思路:利用叉积,三点共线问题。

点a1, a2, a3, a4,a5.....an;

依次求任意两点组成的向量P , 与P共线的点的个数。

 

叉积:向量P,向量Q  若P * Q == 0 则说明向量P , 和Q 共线。

      P (x1, y1), Q(x2, y2)    P * Q = x1*y2- x2*y1;

11306536 NY_lv10 1118 Accepted 256K 469MS C++ 504B 2013-03-03 15:03:50

 

叉积,共线
 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     int n;
 7     int i, j, k;
 8     int t_max, maxx;
 9     int x[800], y[800];
10     while (cin>>n && n!=0)
11     {
12         for (i=0; i<n; i++)
13             cin>>x[i]>>y[i];
14         maxx = 0;
15         for (i=0; i<n; i++)    //点a1
16             for (j=i+1; j<n; j++)  //点a2
17             {
18                 t_max = 0;
19                 for (k=j+1; k<n; k++)   //点a3
20                     //叉积等于0 共线
21                     if ((x[j] - x[i]) * (y[k] - y[i]) - (y[j] - y[i]) * (x[k] - x[i]) == 0)   //a2a1 * a3a1 == 0 则三点共线
22                         
23                         t_max++;
24                     maxx = maxx < t_max ? t_max : maxx;
25             }
26         cout<<maxx+2<<endl;
27     }
28     return 0;
29 }

 

 

 

 

posted @ 2013-03-03 15:14  旅行的蜗牛  阅读(309)  评论(0编辑  收藏  举报