#include <iostream> #include <stdio.h> using namespace std; typedef struct node { double x; double y; }point; typedef struct ed { point a; point b; int is_top; }edge; edge e[100005]; point make_e(point x,point y) { point a; a.x = x.x - y.x; a.y = x.y - y.y; return a; } double cha(point x,point y) { return x.x*y.y - y.x*x.y; } double cross(edge a,point c) { point e1,e2; e1 = make_e(a.a,c); e2 = make_e(a.a,a.b); return cha(e2,e1); } int main() { int n,i,j; while(scanf("%d",&n)&&n) { for(i = 0;i < n;i++) scanf("%lf %lf %lf %lf",&e[i].a.x,&e[i].a.y,&e[i].b.x,&e[i].b.y),e[i].is_top = 0; int count = 0; for(i = 0;i < n;i++) { for(j = i+1;j < n;j++) { if(i != j) { if(cross(e[i],e[j].a)*cross(e[i],e[j].b) <= 0 && cross(e[j],e[i].a)*cross(e[j],e[i].b) <= 0) count++; } } } printf("%d\n",count); } return 0; }
pc代码
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> using namespace std; const int N=102; double eps=0.0000000001; struct node { double x,y; }sh[N],se[N]; int n; double jud(node a,node b,node c) { return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x); } bool cross(int i,int j) { double k1=jud(sh[i],se[i],sh[j]); double k2=jud(sh[i],se[i],se[j]); double k3=jud(sh[j],se[j],sh[i]); double k4=jud(sh[j],se[j],se[i]); if(k1*k2<eps&&k3*k4<eps)return true; return false; } int main() { int i,j,k; while(cin>>n,n) { for(i=0;i<n;i++) { scanf("%lf %lf %lf %lf",&sh[i].x,&sh[i].y,&se[i].x,&se[i].y); } int ans=0; for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(cross(i,j))ans++; } } cout<<ans<<endl; } return 0; }