http://acm.hdu.edu.cn/showproblem.php?pid=1086

You can Solve a Geometry Problem too

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8861    Accepted Submission(s): 4317

Problem Description
Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is very easy, after all we are now attending an exam, not a contest :)
Give you N (1<=N<=100) segments(线段), please output the number of all intersections(交点). You should count repeatedly if M (M>2) segments intersect at the same point.

Note:
You can assume that two segments would not intersect at more than one point. 

 
Input
Input contains multiple test cases. Each test case contains a integer N (1=N<=100) in a line first, and then N lines follow. Each line describes one segment with four float values x1, y1, x2, y2 which are coordinates of the segment’s ending. 
A test case starting with 0 terminates the input and this test case is not to be processed.
 
Output
For each case, print the number of intersections, and one line one case.
 
Sample Input
2
0.00 0.00 1.00 1.00
0.00 1.00 1.00 0.00
3
0.00 0.00 1.00 1.00
0.00 1.00 1.00 0.000
0.00 0.00 1.00 0.00
0
 
题解:本题题干已经排除了两线重合的多边交于一点的情况,故直接枚举所有的边是否相交即可
 #include<cstdio>
#include<cmath>
using namespace std;
#define eps 1e-6
#define N 105
struct point{
double x , y ;
point(double x_, double y_){
x = x_;
y = y_;
}
point(){}
point operator - (const point a) const
{
return point(x-a.x,y-a.y);
}
double operator * (const point a) const
{
return x*a.y - a.x*y;
}
}; struct line{
point s , t;
}L[N]; int main()
{
int T;
while(~scanf("%d",&T),T)
{
for(int i = ;i < T ; i++)
{
scanf("%lf%lf%lf%lf",&L[i].s.x,&L[i].s.y,&L[i].t.x,&L[i].t.y);
}
int ans = ;
for(int i = ; i < T ; i++)
{
for(int j = i+ ; j < T ; j++)//j从i开始保证不会重复判断
{
// if(i==j) continue;
point A = L[i].s;
point B = L[i].t;
point C = L[j].s;
point D = L[j].t;
if((((D-C)*(A-C))*((D-C)*(B-C)))>eps) {continue;}
if((((D-A)*(B-A))*((C-A)*(B-A)))>eps) {continue;}
ans++;
}
}
printf("%d\n",ans);
}
return ;
}

也可以把他们写成函数在外面

 #include <cstdio>
#include <cmath>
using namespace std;
#define eps 1e-8
#define N 105
struct point{
double x, y;
point(){}
point(double _x, double _y) {
x = _x, y = _y;
} point operator - (point a){
return point(x-a.x, y-a.y);
} double operator * (point a){
return x*a.y - y*a.x;
}
}; struct line{
point s, t;
}L[N]; bool ck(line a, line b)
{
point A = a.s, B = a.t, C = b.s, D = b.t;
if(((C-A)*(B-A)) *((D-A)*(B-A)) > eps) return false;
if(((A-C)*(D-C)) *((B-C)*(D-C)) > eps) return false;
return true;
} int main()
{
int n;
while(~scanf("%d", &n), n)
{
for(int i = ; i < n; i++)
scanf("%lf %lf %lf %lf", &L[i].s.x, &L[i].s.y, &L[i].t.x, &L[i].t.y);
int cnt = ;
for(int i = ; i < n; i++)
for(int j = i+; j < n; j++)
cnt += ck(L[i], L[j]);
printf("%d\n", cnt);
}
}

You can Solve a Geometry Problem too(线段求交)的更多相关文章

  1. hdu 1086 You can Solve a Geometry Problem too [线段相交]

    题目:给出一些线段,判断有几个交点. 问题:如何判断两条线段是否相交? 向量叉乘(行列式计算):向量a(x1,y1),向量b(x2,y2): 首先我们要明白一个定理:向量a×向量b(×为向量叉乘),若 ...

  2. HDU1086You can Solve a Geometry Problem too(判断线段相交)

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  3. (hdu step 7.1.2)You can Solve a Geometry Problem too(乞讨n条线段,相交两者之间的段数)

    称号: You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/ ...

  4. You can Solve a Geometry Problem too(判断两线段是否相交)

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  5. (叉积,线段判交)HDU1086 You can Solve a Geometry Problem too

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  6. You can Solve a Geometry Problem too (hdu1086)几何,判断两线段相交

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3276 ...

  7. hdu 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/3 ...

  8. hdu 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/3 ...

  9. HDU 1086:You can Solve a Geometry Problem too

    pid=1086">You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Mem ...

随机推荐

  1. 以深圳.NET俱乐部名义 的技术交流会圆满成功

    2017年5月13日的深圳下着暴雨,一场以深圳.NET俱乐部名义的.NET技术交流会在微软Build 2017刚闭幕时在罗湖布吉路与翠山路交界处富基PARK国际6F举办,这次交流以微软Build 20 ...

  2. CJOJ 血帆海盗

    Description 随着资本的扩大,藏宝海湾贸易亲王在卡利姆多和东部王 国大陆各建立了N/2 个港口.大灾变发生以后,这些港口之间失去了联系,相继脱离了藏宝海湾贸易亲王的管辖,各自为政.利益的驱动 ...

  3. ES6 函数的扩展2

    8.2 rest参数 ES6引入rest参数(形式为"-变量名"),用于获取函数的多余参数,这样就不需要使用arguments对象了. arguments对象并没有数组的方法,re ...

  4. WPF单位真的与分辨率无关吗?

    转载自http://www.cnblogs.com/helloj2ee/archive/2009/04/21/1440709.htm WPF从发布之日起,一直将"分辨率无关(resoluti ...

  5. python 字符串中的%s与format

    你可以选择字符串拼接,你也可以选择使用%s或者是format,下面简单介绍一下它们的使用方法: # 在字符串后面跟%,然后后面加上要被替换的值 print('I like %s' % 'apples' ...

  6. windows下查看端口占用情况及关闭相应的进程

    经常,我们在启动应用的时候发现系统需要的端口被别的程序占用,如何知道谁占有了我们需要的端口,很多人都比较头疼,下面就介绍一种非常简单的方法. 例如:需要查看9001端口被谁占用,并将其进程强制关闭 在 ...

  7. java多线程(七)-线程之间的 协作

    对于多线程之间的共享受限资源,我们是通过锁(互斥)的方式来进行保护的,从而避免发生受限资源被多个线程同时访问的问题.那么线程之间既然有互斥,那么也会有协作.线程之间的协作也是必不可少的,比如 盖个商场 ...

  8. Optimize For Ad Hoc Workloads

    --临时工作负载优化   即席查询:也就是查询完没放到Cache当中,每次查询都要重新经过编译,并发高的时候很耗性能: 参数化查询: 一方面解决了重编译问题,但随着数据库数据数据的变更,统计信息的更新 ...

  9. 每天学一点Docker(2)

    容器runtime 容器runtime是容器真正运行的地方,runtime需要和操作系统kernel紧密结合,为容器提供运行环境. 比如说,java程序比作一个容器,JVM就是runtime.JVM为 ...

  10. JSON 序列化和解析

    概述 JSON 即 (Javascript Object Notation,Javascript 对象表示法),是在Javascript中写结构化数据的方式.而JSON本身只是一种数据格式. 通常开发 ...