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 ; }

最新文章

  1. Key/Value之王Memcached初探:一、掀起Memcached的盖头来
  2. go 数据类型转换
  3. overview
  4. 知方可补不足~UPDLOCK更新锁的使用
  5. Windows 8.1 安装Ruby on Rails手记
  6. 基于@AspectJ和schema的aop(一)
  7. 【linux设备模型】之platform设备驱动
  8. struts2.1笔记06:struts2开发环境的搭建实际操作出现的问题
  9. material-design-library
  10. bzoj3574[Hnoi2014]抄卡组
  11. oracle pipelined返回值函数 针对数据汇总统计 返回结果集方法
  12. mina学习资料整合
  13. UVA 270 Lining Up 共线点 暴力
  14. CSS样式中常用的字体名称
  15. Java基础系列--桶排序
  16. Java多线程之sleep方法阻塞线程-模拟时钟
  17. 第一个微信小程序踩的几个小坑
  18. [转]阿里巴巴十年Java架构师分享,会了这个知识点的人都去BAT了
  19. P4577 [FJOI2018]领导集团问题
  20. jquery 元素筛选 13.6.20

热门文章

  1. BugKu-图穷匕见
  2. 微信小程序简单的数据表格及查询功能
  3. vim中插入递增数
  4. python 打印9*9乘法表
  5. IO Streams:对象流
  6. nyoj 题目6 喷水装置
  7. 201621123034 《Java程序设计》第4周学习总结
  8. NetScaler通过DHCP服务器获取IP地址
  9. BZOJ4002 [JLOI2015]有意义的字符串 【数学 + 矩乘】
  10. BZOJ4196 [Noi2015]软件包管理器 【树剖】