BZOJ1610: [Usaco2008 Feb]Line连线游戏

【传送门:BZOJ1610


简要题意:

  在一个平面直角坐标系上,给出n个点,可以选择两个点来连成一条直线,求出有多少种互不平行的直线


题解:

  用斜率公式,两个点,设两个点的坐标为x1,y1,x2,y2

  那么这两个点所连成的直线的斜率为(y1-y2)/(x1-x2)或(y2-y1)/(x2-x1)

  就可以做这道题了

  注意有可能这两个点所连成的直线平行与x轴或y轴,要特殊处理


参考代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std;
double eps=1e-8;
struct node
{
    double x,y;
}a[210];
double k[41000];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
    int len=0;
    bool xx=false,yy=false;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if(a[i].y-a[j].y==0) xx=true;
            else if(a[i].x-a[j].x==0) yy=true;
            else k[++len]=(a[i].y-a[j].y)/(a[i].x-a[j].x);
        }
    }
    sort(k+1,k+len+1);
    int ans=1;
    for(int i=2;i<=len;i++)
    {
        if(k[i]-k[i-1]==0) continue;
        else ans++;
    }
    if(xx==true) ans++;
    if(yy==true) ans++;
    printf("%d
",ans);
    return 0;
}

 

原文地址:https://www.cnblogs.com/Never-mind/p/8017057.html