JD 1548 平面上的点

本文深入探讨了平面上点与直线之间的关系,通过一组测试数据,展示了如何计算最多有多少个点恰好位于某条直线上。文章详细介绍了算法实现,包括结构体定义、读取数据、计算斜率、获取最大值等关键步骤,为读者提供了一个清晰的数学视角。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:

给定平面上的n个点,任意做一条直线,求至多能有几个点恰好落在直线上。

输入:

包含多组测试数据,每组测试数据由一个整数n(0<=n<=100)开始,代表平面上点的个数。
接下去n行每行给出一个点的坐标(x,y),x、y的绝对值均小于等于100。

输出:

对于每组测试数据,输出一个整数,表示至多能有几个点恰好落在直线上。

样例输入:
2
0 0
1 1
4
0 0
1 1
2 2 
3 6
样例输出:
2
3
代码
#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
#include <set>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define SWAP(a , b){ int temp = a; a = b; b = temp;}
#define Max_N 108

using namespace std;
int N;

struct Node//结构体,计算斜率
{
    float yy;//y1-y2
    float xx;//x1-x2
    Node(){};
    Node(float y,float x):yy(y),xx(x){};
    bool friend operator< (const Node A,const Node B)
    {
        return A.yy * B.xx < A.xx * B.yy;
    }
    bool friend operator== (const Node A,const Node B)
    {
        return A.yy * B.xx == A.xx * B.yy;
    }
};

struct Point//平面上的点
{
    float x;
    float y;
}P[Max_N];

void read()
{
    for(int i = 1;i <= N;i++)
        //scanf("%f%f",&P[i].x,&P[i].y);
        cin>>P[i].x>>P[i].y;
}

int getmax(int id)
{
    map<Node , int> mp;//映射,Node -> int
    map<Node , int>::iterator it;
    mp.clear();
    int ans = 0;
    for(int i = 1;i <= N;i++)
    {
        if(i == id) continue;
        if(P[i].x == P[id].x)
            ans++;
        else
            mp[Node(P[id].y-P[i].y , P[id].x-P[i].x)]++;
    }
    for(it = mp.begin();it != mp.end();it++)
        ans = Max(ans,it->second);
    return ans + 1;
}

int solve()
{
    read();
    int maxnum = 0;
    for(int i = 1;i <= N;i++)
        maxnum = Max(maxnum , getmax(i));
    return maxnum;
}

int main()
{
    while(cin >> N)
    {
        if(N == 0) {cout <<0<<endl;continue;}
        cout << solve() <<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值