85. Three Points On A Line

本文介绍了一种用于判断二维平面上三个点是否共线的算法。通过输入多个测试用例,每个用例包含一系列点的坐标,算法将确定是否存在三点共线的情况。此算法在计算机图形学和几何计算中具有广泛应用。

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

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  appears in the first line of input.

Each test case begins with the number of points . The following  lines describe the coordinates  of each point, in accuracy of at most 3 decimals. Coordinates are ranged in .

输出格式
For each test case, output Yes if there're three points located on the same line, otherwise output No.

输入样例
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

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
struct node
{
    double x;
    double y;
}s[110];
int judge(node a, node b, node c)
{
   if((a.y - b.y)/(a.x - b.x) == (c.y - a.y)/(c.x - a.x))
        return 1;
    return 0;
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n;
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
        {
            scanf("%lf%lf", &s[i].x,&s[i].y);
        }
        int flag = 0;
        for(int i = 0; i < n; i++)
        {
            for(int j = i+1; j < n; j++)
            {
                for(int k = j+1; k < n; k++)
                {
                    if(flag)
                        break;
                    flag = judge(s[i],s[j],s[k]);
                    
                }
            }
        }
        if(flag)
            printf("Yes\n");
        else
            printf("No\n");
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值