题意:给你n个点的坐标。求一条直线最多能穿过多少个点。
思路:枚举(n^2)+求斜率+排序 (复杂度n^2logn)大功告成

//By: Sirius_Ren
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,maxx;double s[705];
struct line{int x,y;}a[705];
bool cmp(line a,line b){return a.x<b.x;}
int main()
{
    while(scanf("%d",&n)&&n){
        maxx=0;
        for(int i=1;i<=n;i++)
            scanf("%d%d",&a[i].x,&a[i].y);
        sort(a+1,a+1+n,cmp);
        for(int i=1;i<n;i++){
            int tot=1,ans=0;
            for(int j=i+1;j<=n;j++)
                s[tot++]=(1.0*a[j].y-1.0*a[i].y)/(1.0*a[j].x-1.0*a[i].x);
            sort(s+1,s+tot);
            for(int i=1;i<tot-1;i++)
                if(fabs(s[i+1]-s[i])<1e-6)ans++,maxx=max(maxx,ans);
                else ans=0;
        }
        printf("%d\n",maxx+2);
    }
}

POJ  1118 求平面上最多x点共线

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-11
  • 2022-02-01
  • 2021-09-15
  • 2022-02-07
  • 2022-02-07
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-07
  • 2021-10-10
  • 2022-02-07
  • 2021-09-15
  • 2021-07-24
相关资源
相似解决方案