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;
}