HDU 1086 You can Solve a Geometry Problem too
生活随笔
收集整理的這篇文章主要介紹了
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/32768 K (Java/Others)
Total Submission(s): 2734 Accepted Submission(s): 1284
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.
?
2 using namespace std;
3 double p[100][2],q[100][2];
4 double direction( double p[] , double q[] , double r[])
5 {
6 return ( (r[0] - p[0]) * (q[1] - p[1]) - (r[1] - p[1]) * (q[0] - p[0]));
7 }
8 bool onsegment(double p[],double q[], double r[])
9 {
10 if(((r[0] - p[0])*(r[0] - q[0]) <= 0) && ((r[1] - p[1]) * (r[1] - q[1]) <= 0))
11 return true;
12 else return false;
13 }
14 bool judge(int i, int j)
15 {
16 double d1 , d2 , d3 , d4 ;
17 d1 = direction(p[i],q[i],p[j]);
18 d2 = direction(p[i],q[i],q[j]);
19 d3 = direction(p[j],q[j],p[i]);
20 d4 = direction(p[j],q[j],q[i]);
21 if( (d1 * d2 < 0) && (d3 * d4 < 0))
22 return true;
23 else if( d1 == 0 && onsegment(p[i],q[i],p[j]) == 1)
24 return true;
25 else if( d2 == 0 && onsegment(p[i],q[i],q[j]) == 1)
26 return true;
27 else if( d3 == 0 && onsegment(p[j],q[j],p[i]) == 1)
28 return true;
29 else if( d4 == 0 && onsegment(p[j],q[j],q[i]) == 1)
30 return true;
31 else return false;
32 }
33 int main(){
34 int n, i , j , count;
35 while( scanf( "%d" , &n ) , n)
36 {
37 for( i = 0 ; i < n ; ++ i )
38 {
39 scanf( "%lf %lf %lf %lf", &p[i][0] , &p[i][1] , &q[i][0] , &q[i][1] ) ;
40 }
41 for( i = 0 , count = 0 ; i < n ; i ++ )
42 for( j = i + 1 ; j < n ; j ++ )
43 if( judge( i , j ) == 1 )
44 ++count;
45 printf("%d\n",count);
46 }
47 return 0;
48 }
總結
以上是生活随笔為你收集整理的HDU 1086 You can Solve a Geometry Problem too的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 快速创建 IEqualityCompar
- 下一篇: mysql数据库同步 debug