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. 解题思路:best time to buy and sell stock i && ii && iii

    这三道题都是同一个背景下的变形:给定一个数组,数组里的值表示当日的股票价格,问你如何通过爱情买卖来发家致富? best time to buy and sell stock i: 最多允许买卖一次 b ...

  2. ZooKeeper如何保证单一视图

    由于ZooKeeper的数据模型简单且全部在内存中,ZooKeeper的速度非常快.它提供了一系列保证: • 顺序一致性 • 原子性 • 单一视图 • 可靠性 • 实时性 下面将结合源码(3.4.10 ...

  3. vue.js之过滤器,自定义指令,自定义键盘信息以及监听数据变化

    一.监听数据变化 1.监听数据变化有两种,深度和浅度,形式如下: vm.$watch(name,fnCb); //浅度 vm.$watch(name,fnCb,{deep:true}); //深度监视 ...

  4. SQL奇技淫巧

    1.SQL行列转换 问题:假设有张学生成绩表(tb)如下:姓名 课程 分数张三 语文 74张三 数学 83张三 物理 93李四 语文 74李四 数学 84李四 物理 94想变成(得到如下结果): 姓名 ...

  5. python实现散列表的链表法

    在散列中,链接法是一种最简单的碰撞解决技术,这种方法的原理就是把散列到同一槽中的所有元素 都放在一个链表中. 链接法有两个定理,定理一: 在简单一致散列的假设下,一次不成功查找的期望时间为O(1 + ...

  6. bash脚本之数组学习

    在bash中可使用索引数组和关联数组,bash在4.0版本之后才添加了对关联数组的支持 一.索引数组 1.定义索引数组 # 方式1 array_value=(1 2 3 4 5 6)或者array_v ...

  7. React日常填坑手册(持续更新)

    1.react中自己定义的组件第一个字母一定要大写,如<app />会不显示,<App />才能正常显示. 2.在react中点击事件里面setState时会使this重新定义 ...

  8. 嵌入式linux下wifi网卡的使用(二)——应用程序iw编译

    首先编译iw,Iw支持两种加密/认证方式.第一种是OPEN/OPEN 第二种是WEP/WEP在网上下载iw源码,发现iw的编译需要依赖libnl库(这个库是为了方便应用程序使用netlink借口而开发 ...

  9. 关于linux下的date日期,并以日期给文件命名

    在linux的终端中,我们输入date后会有以下显示: 然后博主也扩展了一下date的基础用法: date + "%-": %y 输出年份的后2位:%Y 输出完整年份 %m 输出月 ...

  10. Web程序员必备的CSS工具

    对于web开发来说,CSS是最有效的美化页面.设置页面布局的技术.但问题是,CSS是一种标记性语言,语法结构非常的松散.不严谨.WEB程序员会经常发现自己的或别人的CSS文件里有大量的冗余代码或错误或 ...