北邮OJ 85. Three Points On A Line

该博客探讨了在二维平面上判断给定点集是否存在三点共线的问题。通过读取输入的点坐标,计算每组三点的斜率来确定是否在同一直线上。若发现三点半径在[-10^4, 10^4]区间内且共线,则输出,否则输出否定结果。博主提出了两种解决方案,一是枚举所有组合并比较斜率,二是存储已知共线组以减少后续计算。" 108310898,8686410,静态数据表与动态数据的JOIN操作解析,"['数据库理论', 'SQL语言', '数据处理', '数据建模']
摘要由CSDN通过智能技术生成

85. Three Points On A Line

时间限制1000 ms     内存限制 65536 KB    

题目描述

Given points on a 2D plane, judge whether there're three points that locate on the same line.

输入格式

The number of test cases T(1T10)  appears in the first line of input.

Each test case begins with the number of points N(1N100)  . The following N  lines describe the coordinates (x i ,y i )  of each point, in accuracy of at most 3 decimals. Coordinates are ranged in [10 4 ,10 4 ]  .

输出格式

For each test case, output Yes if there're three points located on the same line, otherwise outputNo.

输入样例

2
3
0.0 0.0
1.0 1.0
2.0 2.0
3
0.001 -2.000
3.333 4.444
1.010 2.528

输出样例

Yes
No

 

三点共线,枚举所有的3点,求斜率,一样则存在,做好标记然后退出枚举即可。还有一种做法是求出一组后,把计算结果存起来,减少计算量。

 

#include<stdio.h>
int main(){
    int t,n,i,j,k;
    double x[105],y[105];
    for(scanf("%d",&t);t--;){
        scanf("%d",&n);
        for(i=0;i<n;i++)
            scanf("%lf%lf",x+i,y+i);
        int sig=0;
        for(i=0;i<n&&!sig;i++)
            for(j=i+1;j<n&&!sig;j++){
                for(k=j+1;k<n&&!sig;k++){
                    if((y[k]-y[i])/(x[k]-x[i])==(y[j]-y[i])/(x[j]-x[i]))sig=1;
                }
            }
        if(sig)printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值