题意:给一个多边形柱体的底边形状和体积,求它的高。
题解:叉积求面积,再用体积除以面积。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdlib> 2 #include<cmath> 3 #include<cstdio> 4 #include<algorithm> 5 #define max(a,b) (((a)>(b))?(a):(b)) 6 #define min(a,b) (((a)>(b))?(b):(a)) 7 #define sign(x) ((x)>eps?1:((x)<-eps?(-1):(0))) //符号函数 8 using namespace std; 9 const int MAXN=10000; 10 const double eps=1e-8,inf=1e50; 11 struct point 12 { 13 double x,y; 14 point(){} 15 point(double _x,double _y){x=_x;y=_y;} 16 }; 17 inline double xmult(double x1,double y1,double x2,double y2) 18 { 19 return x1*y2-x2*y1; 20 } 21 inline double getarea(point pg[],int n) 22 { 23 double area=0; 24 pg[n]=pg[0]; 25 for(int i=0;i<n;i++) 26 area+=xmult(pg[i].x,pg[i].y,pg[i+1].x,pg[i+1].y); 27 return fabs(area)/2.0; 28 } 29 point pg[MAXN]; 30 int main() 31 { 32 int n; 33 while(scanf("%d",&n),n>=3) 34 { 35 for(int i=0;i<n;i++) 36 scanf("%lf%lf",&pg[i].x,&pg[i].y); 37 double v; 38 scanf("%lf",&v); 39 printf("BAR LENGTH: %.2lf\n",v/getarea(pg,n)); 40 } 41 return 0; 42 }