You can Solve a Geometry Problem too

                                        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                        

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
 
Sample Output
1
3
 
Author
lcy
 
 
直接O(N^2)判断两线段是否相交即可。
 
判断线段是否相交的模板:
 
 inline double CrossProduct(node a, node b, node c){
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
//Calculate the crossproduct inline bool SegX(node p1, node p2, node p3, node p4){
double d1 = CrossProduct(p3, p4, p1);
double d2 = CrossProduct(p3, p4, p2);
double d3 = CrossProduct(p1, p2, p3);
double d4 = CrossProduct(p1, p2, p4);
return (d1 * d2 <= && d3 * d4 <= );
}
//Judge whether the line segments intersact
 那么直接套用一下就好了。
 
 #include <bits/stdc++.h>

 using namespace std;

 struct node{
double x, y;
} pa[], pb[]; int n, num; inline double CrossProduct(node a, node b, node c){
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
} inline bool SegX(node p1, node p2, node p3, node p4){
double d1 = CrossProduct(p3, p4, p1);
double d2 = CrossProduct(p3, p4, p2);
double d3 = CrossProduct(p1, p2, p3);
double d4 = CrossProduct(p1, p2, p4);
return (d1 * d2 <= && d3 * d4 <= );
} int main(){ while (~scanf("%d", &n), n){
num = ;
for (int i = ; i <= n; ++i) scanf("%lf%lf%lf%lf", &pa[i].x, &pa[i].y, &pb[i].x, &pb[i].y);
for (int i = ; i <= n - ; ++i)
for (int j = i + ; j <= n; ++j)
if (SegX(pa[i], pb[i], pa[j], pb[j])) ++num;
printf("%d\n", num);
} return ; }

HDU1086 You can Solve a Geometry Problem too(计算几何)的更多相关文章

  1. (叉积,线段判交)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 ...

  2. hdu_1086 You can Solve a Geometry Problem too(计算几何)

    http://acm.hdu.edu.cn/showproblem.php?pid=1086 分析:简单计算几何题,相交判断直接用模板即可. 思路:将第k条直线与前面k-1条直线进行相交判断,因为题目 ...

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

  4. 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 ...

  5. 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 ...

  6. (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/ ...

  7. 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 ...

  8. 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. You can Solve a Geometry Problem too(线段求交)

    http://acm.hdu.edu.cn/showproblem.php?pid=1086 You can Solve a Geometry Problem too Time Limit: 2000 ...

随机推荐

  1. RDD算子、RDD依赖关系

    RDD:弹性分布式数据集, 是分布式内存的一个抽象概念 RDD:1.一个分区的集合, 2.是计算每个分区的函数 ,    3.RDD之间有依赖关系 4.一个对于key-value的RDD的Partit ...

  2. 安装 Windows Server 2012 Active Directory 只读域控制器 (RODC)(级别 200)

    安装 Windows Server 2012 Active Directory 只读域控制器 (RODC)(级别 200) 适用对象:Windows Server 2012 本主题介绍如何创建分步的 ...

  3. Intellij IDEA快捷键大全

    Intellij IDEA快捷键大全 Intellij IDEA这个工具有些方面确实比较优秀,使用了一段时间的IntelliJ IDEA,感觉这个JAVA IDE非常好用!比如javascript自动 ...

  4. IntelliJ IDEA 视频教程

    相关视频教程: Intellij IDEA视频教程 最新版Intellij IDEA视频教程

  5. 利用python多线程模块实现模拟接口并发

    import requestsimport jsonimport threadingimport timeimport uuid class postrequests(): def __init__( ...

  6. copy & deepcopy

    1 import copy 2 3 字典参照列表结论,看是否有深层嵌套. 4 a = {'name':1,'age':2} 5 b = a 6 a['name'] = 'ff' 7 print(a) ...

  7. Web 安全问题 rel="noopener nofollw"

    1. noopener 如果你需要用 a 标签打开一个标签页时,你会使用 target='_blank' 这个属性,此时你需要添加 rel='noreferrer noopener' 当你使用 tar ...

  8. Leetcode 640.求解方程

    求解方程 求解一个给定的方程,将x以字符串"x=#value"的形式返回.该方程仅包含'+',' - '操作,变量 x 和其对应系数. 如果方程没有解,请返回"No so ...

  9. 禁用jQuery chosen的选择下拉菜单

    想法是启用被勾掉之后,左侧下拉框禁用.这是chosen()的 disabled之后需要更新一下.就这样,还有别的方法的话请分享,O(∩_∩)O哈哈~

  10. layer 体验

    做后端的,前端一直不擅长,提示语以前也只会alert,非常难看,后来在别人代码看到lay.msg() https://pan.baidu.com/s/1eRHH59g <!DOCTYPE htm ...