牛客网 Kuangyeye's Game 计算几何 直线的一般方程

https://ac.nowcoder.com/acm/contest/3674/F
链接:https://ac.nowcoder.com/acm/contest/3674/F
来源:牛客网

题目描述
Christmas Day is coming! To celebrate this great festival, Kuangyeye, a creative boy, wants to play a game with everyone. The rule is described as following: there are several balloons in the classroom, and you have a prop gun. To achieve a higher goal, you need to shoot as many balloons as you can with one bullet. Now you have to judge whether you can explosive all balloons with one shoot. In a rigorous word, there are n points on the plane, you need to judge if they are on the same line. In addition, the balloons may extremely tiny, so some of them may share the same coordinate.

输入描述:
The first line contains an integer n which indicates the number of balloons. The next n following lines contain two integers xi and yi each, which represent the X coordinate and the Y coordinate of i-th balloon respectively.
输出描述:
If you can explosive all balloons with one shoot, output “Yes”. Output “No” otherwise(without quotes).
示例1
输入
复制
2
1 1
-1 -1
输出
复制
Yes
说明
These two ballons are all on the line x-y=0.
示例2
输入
复制
3
1 2
2 1
3 3
输出
复制
No
说明
We can’t find a line which all these ballons on it.
备注:
1<=n<=1000, |xi|,|yi|<=1000,

题目大意:给 n n n个坐标为整数的点,判断 n n n个点是否共线。
思路:使用直线的一般方程来求解比较简单,百度百科介绍的就挺好的。

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;

struct node
{
    int x,y;
}a[1005];

int n;

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d %d",&a[i].x,&a[i].y);
    if(n==1)
        printf("Yes\n");
    else
    {
        bool flag=1;
        int idx=2;
        while(idx<=n)
        {
            if(a[idx].x!=a[1].x||a[idx].y!=a[1].y)
                break;
            ++idx;
        }
        if(idx<=n)
        {
            int A=a[idx].y-a[1].y;
            int B=a[1].x-a[idx].x;
            int C=a[idx].x*a[1].y-a[idx].y*a[1].x;
            for(int i=1;i<=n;i++)
                if(A*a[i].x+B*a[i].y+C!=0)
                    flag=0;
        }
        if(flag)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值