称号:

You can Solve a Geometry Problem too

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 145 Accepted Submission(s): 100
 
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

题目分析:

简单题。核心还是:推断两条线断是否相交。

代码例如以下:

#include<iostream>
#include <cstdio> using namespace std;
struct Line{
double x1,y1,x2,y2;
}lines[110]; bool isCross(Line a,Line b){
if(((a.x1-b.x1)*(a.y2-b.y1)-(a.x2-b.x1)*(a.y1-b.y1))*((a.x1-b.x2)*(a.y2-b.y2)-(a.x2-b.x2)*(a.y1-b.y2))>0)return false;
if(((b.x1-a.x1)*(b.y2-a.y1)-(b.x2-a.x1)*(b.y1-a.y1))*((b.x1-a.x2)*(b.y2-a.y2)-(b.x2-a.x2)*(b.y1-a.y2))>0)return false;
return true;
} int main(){
int n;
while(scanf("%d",&n)!=EOF,n){
int i;
for(i = 0 ; i < n ; ++i){
scanf("%lf %lf %lf %lf",&lines[i].x1,&lines[i].y1,&lines[i].x2,&lines[i].y2);
} int j;
int ans = 0;
for(i = 0 ; i < n ; ++i){//求n条线段中,相交线段的数量
for(j = i+1; j < n ; ++j){
if(isCross(lines[i],lines[j]) == true){
ans++;
}
}
} printf("%d\n",ans);
} return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

(hdu step 7.1.2)You can Solve a Geometry Problem too(乞讨n条线段,相交两者之间的段数)的更多相关文章

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

  2. 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 1086You can Solve a Geometry Problem too(判断两条选段是否有交点)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086 判断两条线段是否有交点,我用的是跨立实验法: 两条线段分别是A1到B1,A2到B2,很显然,如果 ...

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

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

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

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

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

随机推荐

  1. Android架构设计和软硬整合完整训练

    Android架构设计和软硬整合完整训练 Android架构设计和软硬整合完整训练:HAL&Framework&Native Service&Android Service&a ...

  2. .Net——使用.net内置处理程序处理自己定义节点Demo

    在.net中.由于对不同的节点,都相应着类去对它进行处理,.net里面为了方便.已经内置了一些类供我们使用.使我们在读取配置文件时.不必自己去定义类去处理自己定义的自己定义节点. 以下我们写了这样一个 ...

  3. drupal form 中图片上传

    1.创建url 链接到form $items['qianfeng/add'] = array(     'title' => t('加入信息'),     'page callback' =&g ...

  4. poj2226(最小点覆盖)

    传送门:Muddy Fields 题意:一个由r行c列方格组成的田地,里面有若干个方格充满泥泞,其余方格都是草.要用长度不限,宽度为1的长木板来覆盖这些泥方格,但不能覆盖草地.最少要用多少个长木板. ...

  5. 【 D3.js 入门系列 --- 8 】 对话操作(事件)

    本人的个人博客为: www.ourd3js.com csdn博客为: blog.csdn.net/lzhlzz 转载请注明出处,谢谢. 这一节介绍怎样进行对话的操作,如鼠标单击,鼠标滑过等. 对一个被 ...

  6. ORA-00210 ORA-15001 ORA-15055 ORA-01031: insufficient privileges

    ORA-00210: cannot open the specified control file ORA-00202: control file: &apos;+DATA/posdb/con ...

  7. 每天一个JavaScript实例-递归实现反转数组字符串

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. centos7图形配置 firewall-config

    图形配置 firewall-config centos 7:systemctl stop firewalld.service #停止

  9. hdu3974(线段树+dfs)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 题意:给定点的上下级关系,规定如果给i分配任务a,那么他的所有下属.都停下手上的工作,开始做a. ...

  10. Nginx使用ngx_zeromq模块返回502错误的解决方法

    /*********************************************************************  * Author  : Samson  * Date   ...