题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086
代码:
1 #include <iostream>
2 #include <cstdio>
3 using namespace std;
4 const double eps=1e-10;
5 struct point
6 {
7 double x;
8 double y;
9 };
10 struct line
11 {
12 point a;
13 point b;
14 }p[101];
15 double cross(point a,point b,point c)
16 {
17 //向量ac在ab的方向,顺时针为正
18 return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);
19 }
20 bool cr(line u,line v)
21 {
22 double k1=cross(u.a,u.b,v.a);
23 double k2=cross(u.a,u.b,v.b);
24 double k3=cross(v.a,v.b,u.a);
25 double k4=cross(v.a,v.b,u.b);
26 if(k1*k2<eps&&k3*k4<eps)
27 return true;
28 return false;
29 }
30 int main()
31 {
32 int n,i,j,sum;
33 while(~scanf("%d",&n))
34 {
35 sum=0;
36 if(!n)
37 break;
38 for(i=0;i<n;i++)
39 {
40 scanf("%lf%lf%lf%lf",&p[i].a.x,&p[i].a.y,&p[i].b.x,&p[i].b.y);
41 }
42 for(i=0;i<n-1;i++)
43 for(j=i+1;j<n;j++)
44 if(cr(p[i],p[j]))
45 sum++;
46 printf("%d\n",sum);
47 }
48 return 0;
49 }
2 #include <cstdio>
3 using namespace std;
4 const double eps=1e-10;
5 struct point
6 {
7 double x;
8 double y;
9 };
10 struct line
11 {
12 point a;
13 point b;
14 }p[101];
15 double cross(point a,point b,point c)
16 {
17 //向量ac在ab的方向,顺时针为正
18 return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);
19 }
20 bool cr(line u,line v)
21 {
22 double k1=cross(u.a,u.b,v.a);
23 double k2=cross(u.a,u.b,v.b);
24 double k3=cross(v.a,v.b,u.a);
25 double k4=cross(v.a,v.b,u.b);
26 if(k1*k2<eps&&k3*k4<eps)
27 return true;
28 return false;
29 }
30 int main()
31 {
32 int n,i,j,sum;
33 while(~scanf("%d",&n))
34 {
35 sum=0;
36 if(!n)
37 break;
38 for(i=0;i<n;i++)
39 {
40 scanf("%lf%lf%lf%lf",&p[i].a.x,&p[i].a.y,&p[i].b.x,&p[i].b.y);
41 }
42 for(i=0;i<n-1;i++)
43 for(j=i+1;j<n;j++)
44 if(cr(p[i],p[j]))
45 sum++;
46 printf("%d\n",sum);
47 }
48 return 0;
49 }