hdu 1086 A - You can Solve a Geometry Problem too (线段的规范相交非规范相交)
生活随笔
收集整理的這篇文章主要介紹了
hdu 1086 A - You can Solve a Geometry Problem too (线段的规范相交非规范相交)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
A -?You can Solve a Geometry Problem too Time Limit:1000MS?????Memory Limit:32768KB?????64bit IO Format:%I64d & %I64u Submit?Status
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.?
A test case starting with 0 terminates the input and this test case is not to be processed.?
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 0Sample Output
1 3 給N個線段。問交點總數。 多條線段相交于同一個點算多次。 規(guī)范相交和非規(guī)范相交都算。 規(guī)范相交就是,交點不能是線段的端點。 而非規(guī)范相交可以。 1 /************************************************************************* 2 > File Name: code/hdu/1086.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年11月06日 星期五 13時41分46秒 6 ************************************************************************/ 7 8 #include<iostream> 9 #include<iomanip> 10 #include<cstdio> 11 #include<algorithm> 12 #include<cmath> 13 #include<cstring> 14 #include<string> 15 #include<map> 16 #include<set> 17 #include<queue> 18 #include<vector> 19 #include<stack> 20 #include<cctype> 21 #define fst first 22 #define lson l,m,rt<<1 23 #define rson m+1,r,rt<<1|1 24 #define ms(a,x) memset(a,x,sizeof(a)) 25 using namespace std; 26 const int dx4[4]={1,0,0,-1}; 27 const int dy4[4]={0,-1,1,0}; 28 const double eps = 1E-8; 29 typedef long long LL; 30 #define sec second 31 const int inf = 0x3f3f3f3f; 32 const int N=105; 33 int n; 34 int dblcmp(double d) 35 { 36 return d<-eps?-1:d>eps; 37 } 38 39 struct point 40 { 41 double x,y; 42 point(){} 43 point (double _x,double _y): 44 x(_x),y(_y){}; 45 void input() 46 { 47 scanf("%lf%lf",&x,&y); 48 } 49 point sub(point p) 50 { 51 return point(x-p.x,y-p.y); 52 } 53 double det(point p) 54 { 55 return x*p.y-y*p.x; 56 } 57 double dot(point p) 58 { 59 return x*p.x+y*p.y; 60 } 61 }; 62 63 struct line 64 { 65 point a,b; 66 void input() 67 { 68 a.input(); 69 b.input(); 70 } 71 int segcrossseg(line v) 72 { 73 int d1=dblcmp(b.sub(a).det(v.a.sub(a))); 74 int d2=dblcmp(b.sub(a).det(v.b.sub(a))); 75 int d3=dblcmp(v.b.sub(v.a).det(a.sub(v.a))); 76 int d4=dblcmp(v.b.sub(v.a).det(b.sub(v.a))); 77 if ((d1^d2)==-2&&(d3^d4)==-2) return 2; 78 return (d1==0&&dblcmp(v.a.sub(a).dot(v.a.sub(b)))<=0|| 79 d2==0&&dblcmp(v.b.sub(a).dot(v.b.sub(b)))<=0|| 80 d3==0&&dblcmp(a.sub(v.a).dot(a.sub(v.b)))<=0|| 81 d4==0&&dblcmp(b.sub(v.a).dot(b.sub(v.b)))<=0); 82 } 83 }li[N]; 84 int main() 85 { 86 #ifndef ONLINE_JUDGE 87 freopen("in.txt","r",stdin); 88 #endif 89 90 while (~scanf("%d",&n)!=EOF&&n) 91 { 92 for ( int i = 1 ; i <= n ; i++) li[i].input(); 93 int cnt = 0; 94 for ( int i = 1 ; i < n ; i++) 95 for ( int j = i+1 ; j <= n ; j++) 96 { 97 98 if (li[i].segcrossseg(li[j])) 99 cnt++; 100 } 101 printf("%d\n",cnt); 102 } 103 104 105 106 #ifndef ONLINE_JUDGE 107 fclose(stdin); 108 #endif 109 return 0; 110 } View Code?
??
轉載于:https://www.cnblogs.com/111qqz/p/4942531.html
總結
以上是生活随笔為你收集整理的hdu 1086 A - You can Solve a Geometry Problem too (线段的规范相交非规范相交)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小剧场话剧《性情男女》
- 下一篇: SessionListener失败,退出