【题意】
分别给你一个边长相等的三棱锥,四棱锥,五棱锥,并给出各边长,求总体积和
【分析】
椎体的面积是S*h/3,给出各边边长很容易求得底面积S,然后再求出h即可。一开始看错题意,以为给的是h,下面代码是直接改的原来的代码,未免有些麻烦。
显然顶点在底面的投影点是正n边形的中心。
然后根据可以设中心到到边上点的距离为x,t为各边边长,中心与边上相邻两点连线形成的夹角为p,那么显然底面积S=n*三角形面积=n*1/2*x*x*sin(p)、t=2*x*sin(p/2)、h=sqrt(t^2-x^2)、p=2*PI/n
其中n,t已知由以上各式可解出S,h并求出各椎体体积
【Code】
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
typedef long long LL;
const double PI = acos(-1.0);
using namespace std;
double calc(double t,double n,double p)
{
double x = t/(2*sin(p/2));
double h = sqrt(t*t-x*x);
return n*x*x*h*sin(p)/6;
}
int main()
{
double s1, s2, s3;
double h1,h2,h3;
scanf("%lf%lf%lf",&h1,&h2,&h3);
s1 = calc(h1,3,PI*2/3);
s2 = calc(h2,4,PI/2);
s3 = calc(h3,5,PI*2/5);
printf("%.12f",s1+s2+s3);
return 0;
}