标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8861 Accepted Submission(s): 4317
1 #include<cstdio> 2 #include<cmath> 3 using namespace std; 4 #define eps 1e-6 5 #define N 105 6 struct point{ 7 double x , y ; 8 point(double x_, double y_){ 9 x = x_; 10 y = y_; 11 } 12 point(){} 13 point operator - (const point a) const 14 { 15 return point(x-a.x,y-a.y); 16 } 17 double operator * (const point a) const 18 { 19 return x*a.y - a.x*y; 20 } 21 }; 22 23 struct line{ 24 point s , t; 25 }L[N]; 26 27 int main() 28 { 29 int T; 30 while(~scanf("%d",&T),T) 31 { 32 for(int i = 0 ;i < T ; i++) 33 { 34 scanf("%lf%lf%lf%lf",&L[i].s.x,&L[i].s.y,&L[i].t.x,&L[i].t.y); 35 } 36 int ans = 0; 37 for(int i = 0 ; i < T ; i++) 38 { 39 for(int j = i+1 ; j < T ; j++)//j从i开始保证不会重复判断 40 { 41 // if(i==j) continue; 42 point A = L[i].s; 43 point B = L[i].t; 44 point C = L[j].s; 45 point D = L[j].t; 46 if((((D-C)*(A-C))*((D-C)*(B-C)))>eps) {continue;} 47 if((((D-A)*(B-A))*((C-A)*(B-A)))>eps) {continue;} 48 ans++; 49 } 50 } 51 printf("%d\n",ans); 52 } 53 return 0; 54 }
也可以把他们写成函数在外面
1 #include <cstdio> 2 #include <cmath> 3 using namespace std; 4 #define eps 1e-8 5 #define N 105 6 struct point{ 7 double x, y; 8 point(){} 9 point(double _x, double _y) { 10 x = _x, y = _y; 11 } 12 13 point operator - (point a){ 14 return point(x-a.x, y-a.y); 15 } 16 17 double operator * (point a){ 18 return x*a.y - y*a.x; 19 } 20 }; 21 22 struct line{ 23 point s, t; 24 }L[N]; 25 26 bool ck(line a, line b) 27 { 28 point A = a.s, B = a.t, C = b.s, D = b.t; 29 if(((C-A)*(B-A)) *((D-A)*(B-A)) > eps) return false; 30 if(((A-C)*(D-C)) *((B-C)*(D-C)) > eps) return false; 31 return true; 32 } 33 34 int main() 35 { 36 int n; 37 while(~scanf("%d", &n), n) 38 { 39 for(int i = 0; i < n; i++) 40 scanf("%lf %lf %lf %lf", &L[i].s.x, &L[i].s.y, &L[i].t.x, &L[i].t.y); 41 int cnt = 0; 42 for(int i = 0; i < n; i++) 43 for(int j = i+1; j < n; j++) 44 cnt += ck(L[i], L[j]); 45 printf("%d\n", cnt); 46 } 47 }
You can Solve a Geometry Problem too(线段求交)
标签:
原文地址:http://www.cnblogs.com/shanyr/p/4687256.html