题目要求面积和判断非相邻边不相交。和数学和几何有关系。
1 #include <stdio.h> 2 #include <math.h> 3 4 #define MISS 0.0000001 5 6 int det(double x1,double y1,double x2,double y2,double x3,double y3){ 7 double w1,w2; 8 w1=x1*y2+x2*y3+x3*y1; 9 w2=x2*y1+x3*y2+x1*y3; 10 if(fabs(w1-w2)<MISS) 11 return 0; 12 else if(w1>w2) 13 return 1; 14 else if(w1<w2) 15 return -1; 16 } 17 18 int main(){ 19 int n,i,j,count=0,temp; 20 double x[1000],y[1000],area; 21 while(scanf("%d",&n)&&n){ 22 count++; 23 if(count>1) 24 printf("\n"); 25 printf("Figure %d: ",count); 26 for(i=0;i<n;i++) 27 scanf("%lf%lf",&x[i],&y[i]); 28 if(n<3){ 29 printf("Impossible\n"); 30 continue; 31 } 32 for(i=0;i<n;i++){ 33 for(j=(i+1)/n;j<i-1;j++){ 34 temp=(i+1)%n; 35 if (det(x[j],y[j],x[i],y[i],x[j+1],y[j+1])*det(x[j],y[j],x[temp],y[temp],x[j+1],y[j+1])<=0&& 36 det(x[i],y[i],x[j],y[j],x[temp],y[temp])*det(x[i],y[i],x[j+1],y[j+1],x[temp],y[temp])<=0){ 37 printf("Impossible\n"); 38 goto quit; 39 } 40 } 41 } 42 area=0; 43 for(i=0;i<n;i++) 44 area+=x[i]*y[(i+1)%n]-y[i]*x[(i+1)%n]; 45 printf("%.2lf\n",fabs(area)/2); 46 quit:; 47 } 48 return 0; 49 }
时间: 2024-09-29 19:55:19