UVA 11227 The silver bullet.(简单题:枚举经过最多的点的直线)

UVA 11227 The silver bullet.(简单题:枚举经过最多的点的直线)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2168

题意:

       平面上有n个点,问你一条直线最多能经过多少个点?

分析:

       首先对点集去重,然后直接枚举两个点,然后用该点的直线去判断,看看该直线与多少个点相交即可.

AC代码:

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=100+5;
const double eps=1e-10;
int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    return x<0?-1:1;
}
struct Point
{
    double x,y;
    Point(){}
    Point(double x,double y):x(x),y(y){}
    bool operator==(const Point &rhs)const
    {
        return dcmp(x-rhs.x)==0 && dcmp(y-rhs.y)==0;
    }
    bool operator<(const Point &rhs)const
    {
        return x<rhs.x ||( dcmp(x-rhs.x)==0 && y<rhs.y);
    }
}P[maxn];
typedef Point Vector;
Vector operator-(Point A,Point B)
{
    return Vector(A.x-B.x,A.y-B.y);
}
double Cross(Vector A,Vector B)
{
    return A.x*B.y-A.y*B.x;
}
bool InLine(Point P,Point A,Point B)//判断点P在直线AB上
{
    return dcmp(Cross(P-A,A-B))==0;
}

int main()
{
    int T; scanf("%d",&T);
    for(int kase=1;kase<=T;++kase)
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;++i)
            scanf("%lf%lf",&P[i].x,&P[i].y);
        sort(P,P+n);
        n=unique(P,P+n)-P;
        if(n==1)
        {
            printf("Data set #%d contains a single gnu.\n",kase);
            continue;
        }
        int ans=0;
        for(int i=0;i<n;++i)
        for(int j=i+1;j<n;++j)
        {
            int num=0;
            for(int k=0;k<n;++k)
                if(InLine(P[k],P[i],P[j])) ++num;
            ans=max(ans,num);
        }
        printf("Data set #%d contains %d gnus, out of which a maximum of %d are aligned.\n",kase,n,ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值