天天写算法之You can Solve a Geometry Problem too

1人阅读 评论(0) 收藏 举报
分类:

这个题目,依旧使用了cha乘,逆时针的性质,判断两个线段是否相交,在这个地方,只需要判断对于其中任意一个线段,另一个线段的chacheng都为非正数,这里就可以得到两个线段相交了。

代码:

#include<iostream>
#include<string.h>
#include<cstdio>
#include<cmath>
#define MAX 105
using namespace std;

struct point{
    double x ;
    double y ;
};
struct line{
 point from;
 point to ;
}A[MAX];

double product(point a , point b , point c)
{
        double x1,y1,x2,y2;
        x1 = a.x-c.x,y1= a.y-c.y;
        x2 = b.x-c.x,y2= b.y-c.y;
        return x1*y2-y1*x2;
}
//首先题目中说明了,你不会重叠
//这个地方一定要注意《0,因为当两个点正好端点相交,那么就是《0的情况
bool judge(line a ,line b)
{
    return product(a.from,a.to,b.from)*product(a.from,a.to,b.to)<=0&&product(b.from,b.to,a.from)*product(b.from,b.to,a.to)<=0;
}

int main(){
    int n ,i,j,k,ans;
    while(scanf("%d",&n),n)
    {
        for(int i = 0 ; i < n ; i ++ )
        {
            scanf("%lf%lf%lf%lf",&A[i].from.x,&A[i].from.y,&A[i].to.x,&A[i].to.y);
        }
        ans = 0 ;
        for(int i = 0 ; i<n ; i++)
        {
            for(int j = i+1;j<n;j++)
            {
                if(judge(A[i],A[j])) ans++;
            }
        }
        printf("%d\n",ans);
    }
}

查看评论

hdoj-1086-You can Solve a Geometry Problem too 判断线段是否相交

You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/327...
  • qq_18062811
  • qq_18062811
  • 2015-07-20 19:02:23
  • 405

hdoj1086You can Solve a Geometry Problem too【线段相交判断】

You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3...
  • R1986799047
  • R1986799047
  • 2015-09-22 13:51:14
  • 264

杭电OJ——1086 You can Solve a Geometry Problem too

You can Solve a Geometry Problem too Problem Description Many geometry(几何)problems were de...
  • lishuhuakai
  • lishuhuakai
  • 2012-12-06 06:13:10
  • 2365

You can Solve a Geometry Problem too

You can Solve a Geometry Problem too Time Limit:1000MS     Memory Limit:32768KB     64bit IO Fo...
  • u012866104
  • u012866104
  • 2014-01-04 18:45:20
  • 470

杭电5858 (Hard problem) 杭电5867(Water problem)

杭电5858 (Hard problem) Problem Description cjj is fun with math problem. One day he found a Olymp...
  • ESESZB
  • ESESZB
  • 2016-08-18 16:09:10
  • 357

错误The Network Adapter could not establish the connection

Io 异常: The Network Adapter could not establish the connection 这个异常的出现一般与数据库和你的PC的设置有关 这种异常的出现大致上...
  • cnboynet
  • cnboynet
  • 2011-11-23 11:09:44
  • 353

hdu-1029 Ignatius and the Princess IV

hdu-1029 Ignatius and the Princess IV题目描述 Ignatius and the Princess IV Time Limit: 2000/1000 ...
  • oYinGui1
  • oYinGui1
  • 2016-12-16 16:51:28
  • 463

B - Can you solve this equation?

Description 现在,给出等式8* X^4+ 7* X^3+ 2* X^2+ 3 * X +6= Y,请找出他在0和100之间的解(包含0和100)。 现在,请你试试运气。。。。Input...
  • geekword
  • geekword
  • 2016-07-21 19:38:28
  • 528

hdu-1086You can Solve a Geometry Problem too

线段相交-----模板 You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Mem...
  • yinzm520
  • yinzm520
  • 2013-12-05 20:53:01
  • 506

HDU-1086-You can Solve a Geometry Problem too

You can Solve a Geometry Problem too Problem Description Many geometry(几何)problems were de...
  • u013476556
  • u013476556
  • 2014-08-14 10:00:49
  • 563
    个人资料
    持之以恒
    等级:
    访问量: 1775
    积分: 1130
    排名: 4万+
    文章存档
    最新评论