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): 6997    Accepted Submission(s): 3385

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
这是一道几何题。 就是在于你是否会推断两条直线直接是否有交点的方法。
剩下就非常easy了。
推断AB和CD两线段是否有交点:
同一时候满足两个条件:('x'表示叉积)
1.C点D点分别在AB的两側.(向量(ABxAC)*(ABxAD)<=0)
2.A点和B点分别在CD两側.(向量(CDxCA)*(CDxCB)<=0)
</pre><pre name="code" class="cpp">
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath> using namespace std; struct Node
{
double x1, y1, x2, y2;
}point[105];
int n; double work(double x1, double y1, double x2, double y2)
{
return x1 * y2 - x2 * y1;
} bool judge(int i, int j)
{
double a = work(point[i].x1 - point[j].x1, point[i].y1 - point[j].y1, point[j].x2 - point[j].x1, point[j].y2 - point[j].y1);
double c = work(point[j].x2 - point[i].x1, point[j].y2 - point[i].y1, point[i].x2 - point[i].x1, point[i].y2 - point[i].y1);
double b = work(point[i].x2 - point[j].x1, point[i].y2 - point[j].y1, point[j].x2 - point[j].x1, point[j].y2 - point[j].y1);
double d = work(point[j].x1 - point[i].x1, point[j].y1 - point[i].y1, point[i].x2 - point[i].x1, point[i].y2 - point[i].y1);
a = a * b;
c = c * d;
if(a <= 0 && c <= 0)
return true;
return false;
} int main()
{
while(cin >> n, n){
for(int i = 0; i < n; i++)
cin >> point[i].x1 >> point[i].y1 >> point[i].x2 >> point[i].y2;
int count = 0;
for(int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
if(judge(i, j))
count++;
}
cout << count << endl;
}
return 0;
}


HDU 1086:You can Solve a Geometry Problem too的更多相关文章

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

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

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

  4. hdu 1086 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/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. 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 ...

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

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

  9. HDUOJ1086You can Solve a Geometry Problem too

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

随机推荐

  1. hdu2818行列匹配+排序

    题意:给定一个矩阵,矩阵上有的数字是1,有的是0,给定两种操作,交换某两行或者某两列,问是否能置换出对角线为1的矩阵 题解:能够置换出对角线是1的矩形要求有n个1既不在同一行也不再同一列,即行列匹配, ...

  2. Naive Bayes Classification

    Maching Learning QQ群:2 请说明来自csdn 微信:soledede

  3. Could not load file or assembly&#39;System.Data.SQLite.dll&#39; or one of its depedencies

    [问题]  在我本机的开发环境c#连接sqlite3没有问题,但是release版本号移植到其它的机器就提示Could not load file or assembly'System.Data. ...

  4. 【UFLDL】多层神经网络

    请参见原始英文教程地址:http://ufldl.stanford.edu/tutorial/supervised/MultiLayerNeuralNetworks 本文是在学习该教程时记得笔记,供參 ...

  5. 基本调试命令 - u/ub/uf

    原:http://www.cnblogs.com/developersupport/p/windbgcommand-u.html 在调试过程中难免会遇到须要反编译代码来分析逻辑的时候.在windbg中 ...

  6. 第1周 SQL Server 如何执行一个查询

    原文:第1周 SQL Server 如何执行一个查询 大家好,欢迎来到第1周的SQL Server性能调优培训.在我们进入SQL Server性能调优里枯燥难懂的细节内容之前,我想通过讲解SQL Se ...

  7. IIS在W7下使用

    1.0.发布程序

  8. 自己定义actionbar

    android中的actionbar可提供自己定义view.详细是先写好自己定义view的布局,然后在代码中获取Actionbar对象.调用 setCustomView方法. 可是这样,它还是会显示前 ...

  9. XMPP得知--建立一个管理类

    参考其他demo之后,设立一个管理类的发现看起来更舒服,理-- 但在建立与server连接其中.发现 Connect Error: {     NSLocalizedDescription = &qu ...

  10. ZOJ 3635 Cinema in Akiba[ 大规模阵列 ]

    门户:problemCode=3635">ZOJ 3635 Cinema in Akiba Time Limit: 3 Seconds      Memory Limit: 65536 ...