判断几个点是否在一条直线上(计算几何)

链接:http://www.luogu.org/problem/show?pid=1142

题意:n个点,横坐标+纵坐标。判断最多有多少个点能再一条直线上(n <= 700)

解析:求任意两个点构成的直线的方程

化为一般式:ax+by+c=0 得:

 a=y2-y1

 b=x1-x2//注意别写反了

c=-ax1+by1
即可

不可思议的n = 700时n ^3也能过

#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
using namespace std;

int x[705],y[705];
int main()
{
  //freopen("in.txt","r",stdin);

    int n;
    scanf("%d",&n);
    for(int i = 0; i < n; i ++)
    {
        scanf("%d%d",&x[i],&y[i]);
    }
    if(n == 1){
        printf("1\n");
    return 0;}
    int a,b,c;
    int temp;
    int ans = 1;
    for(int i = 0; i < n; i ++)
    {
        for(int j = 0; j < i; j ++)
        {

            a= y[j] - y[i];
            b = x[j] - x[i];
            //C=-ax1+by1
            c = -a*x[i] + b*y[i];
            temp =0;
            for(int t = 0; t < n; t ++)
            {

                if(a*x[t] -b*y[t] + c == 0)
                {
                    temp ++;
                    //   cout<<1<<endl;
                }
            }
           // cout<<temp<<endl;
            ans = max(ans,temp);

        }
    }

    printf("%d\n",ans);
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值