poj P2606 Rabbit hunt

来源:互联网 发布:淘宝寄到澳洲 编辑:程序博客网 时间:2024/05/03 08:48

题目大意:
给出N个点,秋有多少条共线。
1<=N<=200

题解:
暴力枚举+叉积:
1.以任意2个不同的点构建一条直线。
2.用叉积 m=(x-x1)(y2-y1)-(x2-x1)*(y-y1)
枚举除这2个点以外的所有点有多少个在这条直线上,并累加ans。(ans初值为2,因为一开始构建直线时就有2个点)
3.每一次枚举完一条直线上的点数,就比较ans更新答案max找最大值。
时间复杂度:O(N^3)

var      x,y:array [0..201] of longint;      n,i,j,k,ans,max:longint;begin      readln(n);      for i:=1 to n do          readln(x[i],y[i]);      max:=0;      for i:=1 to n do          for j:=1 to n do              begin                   ans:=2;                   for k:=1 to n do                       if (i<>j) and (j<>k) and (i<>k) then                          if (x[i]-x[k])*(y[j]-y[k])-(x[j]-x[k])*(y[i]-y[k])=0                             then inc(ans);                   if ans>max then max:=ans;              end;      writeln(max);end.
1 0