HDOJ1411:按照一定顺序给出四面体的六条边长,求四面体体积。
公式题目:
计算的时候注意把边和角对应好,sqrt里面注意浮点误差,不要对负数开方。
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
double a,b,c,d,e,f;
double CalAng(double a,double b,double c)
{
double ans=(a*a+b*b-c*c)/(2.0*a*b);
ans=acos(ans);
return ans;
}
void work()
{
double W,A,B,C,V;
A=CalAng(a,b,d);
B=CalAng(b,c,f);
C=CalAng(a,c,e);
W=(A+B+C)/2.0;
V=a*b*c*sqrt(sin(W)*sin(W-A)*sin(W-B)*sin(W-C));
V/=3.0;
printf("%0.4lf\n",V);
}
int main()
{
while(scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f)==6)
work();
return 0;
}
同理,组队赛第三场那道题:
已知一个三角形ABC三点在空间内的坐标,和空间某一点q到ABC三点的距离,求q点到ABC的距离。
同样方法求出体积再求底面积高就出来了。
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
struct Point
{
double x;
double y;
double z;
};
double dist(Point a, Point b)
{
double res = sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y) + (a.z - b.z)*(a.z - b.z));
return res;
}
double CalAng(double a, double b, double c)
{
double ans = (a*a + b*b - c*c) / (2.0*a*b);
ans = acos(ans);
return ans;
}
double eps_sin(double x)
{
double res = sin(x);
if (fabs(res) < 1e-6) res = 0;
return res;
}
double CalArea(double a, double b, double c)
{
double p = (a + b + c) / 2.0;
double res = sqrt(fabs(p*(p-a)*(p-b)*(p-c)));
return res;
}
void work()
{
Point P[4];
double a, b, c, d, e, f,h;
double W, A, B, C, V;
for (int i = 1; i<4; i++)
scanf("%lf%lf%lf", &P[i].x, &P[i].y, &P[i].z);
scanf("%lf%lf%lf",&a, &b, &c);
d = dist(P[1], P[2]);
e = dist(P[1], P[3]);
f = dist(P[2], P[3]);
A = CalAng(a, b, d);
B = CalAng(b, c, f);
C = CalAng(a, c, e);
W = (A + B + C) / 2.0;
V = a*b*c*sqrt(eps_sin(W)*eps_sin(W - A)*eps_sin(W - B)*eps_sin(W - C));
h = V / CalArea(d,e,f);
printf("%0.2lf\n",h);
}
int main()
{
int T;
scanf("%d", &T);
while (T--) work();
return 0;
}