HDU 1086 You can Solve a Geometry Problem too HDU 1147 Pickup sticks

这两道题都是规范相交的模板题。

HDU 1086

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1086

View Code
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 struct node
 4 {
 5    double x,y;
 6 }start[124],end[124];
 7 double eps=0.0000000001;
 8 double judge(node p1,node p2,node p )//判断点是否在直线的两边
 9 {
10      return (p1.x-p.x)*(p2.y-p.y )-(p2.x-p.x)*(p1.y-p.y);
11 }
12 bool segments( node p1,node p2,node q1,node q2)
13 {
14       double d1=judge( p1, p2, q1 );
15       double d2=judge( p1, p2, q2 );
16       double d3=judge( q1, q2, p1 );
17       double d4=judge( q1, q2 ,p2 );
18       if( d1*d2<eps&&d3*d4<eps )
19       return true;
20       return false;
21 }
22 int main()
23 {
24 
25     int n,i,j;
26     while(~scanf("%d",&n))
27     {
28         if(!n)
29         break;
30         int sum=0;
31         for(i=0; i<n; i++ )
32         {
33              scanf( "%lf%lf%lf%lf",&start[i].x,&start[i].y,&end[i].x,&end[i].y );
34         }
35         for(i=0; i<n-1; i++ )
36            for(j=i+1; j<n; j++ )
37            {
38                if(segments(start[i],end[i],start[j],end[j]))
39                   sum++;
40            }
41          printf("%d\n",sum);
42     }
43    return 0;
44 }

HDU 1147

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1147

View Code
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 struct node
 5 {
 6    double x,y;
 7 }start[200010],end[200010];
 8 double judge(node p1,node p2,node p)//判断点是否在直线的两边
 9 {
10     return ((p1.x-p.x)*(p2.y-p.y)-(p2.x-p.x)*(p1.y-p.y));
11 }
12 /*inline bool on_megment(node p1,node p2,node p)//判断端点是不是在直线上
13 {
14    double max=p1.x>p2.x?p1.x:p2.x;//找出直线的左右端点的范围
15 double min=p1.x<p2.x?p1.x:p2.x;
16    if( p.x>=min&&p.x<=max )   return true;
17    else
18    return false;
19 }*/
20 bool megment( node p1,node p2,node q1,node q2 )
21 {
22     double d1=judge( p1,p2,q1 );
23     double d2=judge( p1,p2,q2 );
24     double d3=judge( q1,q2,p1 );
25     double d4=judge( q1,q2,p2 );
26     if( d1*d2<0&&d3*d4<0 )return true;//如果都异侧就一定相交
27 /*if( d1==0&&on_megment( p1,p2,q1 ) )  returntrue;//d为0是平行的情况,这是我们就要考虑是不是端点在直线上
28 if( d2==0&&on_megment( p1,p2,q2 ) )  returntrue;
29     if( d3==0&&on_megment( q1,q2,p1 ) )  returntrue;
30     if( d4==0&&on_megment( q1,q2,p2 ) )  returntrue;*/
31     return false;
32 }
33 int main()
34 {
35     int n,hash[100010];
36 
37     while( scanf( "%d",&n ),n )
38     {
39        memset( hash, 0,sizeof( hash ) );
40        int count=0;
41        for( int i=1; i<=n; i++ )
42         scanf( "%lf%lf%lf%lf",&start[i].x,&start[i].y,&end[i].x,&end[i].y );
43         for( int i=1;i<=n; i++ )
44            for( int j=i+1;j<=n; j++ )
45            {
46                 if( megment( start[i],end[i],start[j],end[j] ) )
47                 {
48                      hash[i]=1;//记录被覆盖的棍子
49                      count++;
50                      break;
51                 }
52            }
53            int sum=0;
54            count=n-count;
55            printf( "Top sticks:" );
56            for( int i=1;i<=n; i++ )
57            {
58               if( 0==hash[i] )
59               {
60                  sum++;
61                  printf( count==sum?" %d.\n":" %d,",i );
62               }
63            }
64     }
65     return 0;
66 }

计算几何的参考资料http://www.cnblogs.com/jbelial/archive/2011/08/04/2127487.html

原文地址:https://www.cnblogs.com/timeship/p/2640801.html