您的位置:首页 > 其它

UVA 10522 Height to Area

2011-06-26 12:14 260 查看
一直三角形三个高,求这个三角形的面积。



我自己推的时候没耐心。。没推出来。。。百度后,发现 有公式。。。T T 。。。。贴一下。



设对应三边为a,b,c

面积S=1/2ax=1/2by=1/2cz
a=2S/x b=2S/y c=2S/z

使用求积公式:
S=√[m(m-a)(m-b)(m-c)]

其中

m=1/2(a+b+c) =S( 1/x + 1/y+1/z)

m-a=S(1/y+1/z-1/x)

m-b=S(1/x +1/z-1/y)

m-c=S(1/x + 1/y-1/z)

代入得 S=S2√[( 1/x + 1/y+1/z)(1/y+1/z-1/x)(1/x +1/z-1/y)(1/x + 1/y-1/z)]

S=1/√[( 1/x + 1/y+1/z)(1/y+1/z-1/x)(1/x +1/z-1/y)(1/x + 1/y-1/z)]



不满足题意的就是s这个下面的分子等于0或者根号里面的小于0,或者x y z为0,判断一下就好,注意控制精度我的1e-6过不去,改成1e-8就过了。



#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
using namespace std;
const double eps = 1e-8;
bool dy(double x,double y)	{	return x > y + eps;}	// x > y 
bool xy(double x,double y)	{	return x < y - eps;}	// x < y 
bool dyd(double x,double y)	{ 	return x > y - eps;}	// x >= y 
bool xyd(double x,double y)	{	return x < y + eps;} 	// x <= y 
bool dd(double x,double y) 	{	return fabs( x - y ) < eps;}  // x == y
int main()
{
	int ncases;
	double x,y,z;
	
	scanf("%d",&ncases);
	
	while( ncases )
	{
		scanf("%lf%lf%lf",&x,&y,&z);
		if( dd(x,0.0) || dd(y,0.0) || dd(z,0.0) )
		{
			printf("These are invalid inputs!/n");
			ncases--;
			continue;
		}
		double p =  (1/x +  1/y + 1/z)*(1/y + 1/z - 1/x)*(1/x + 1/z - 1/y)*(1/x + 1/y - 1/z);
		if( xyd(p,0.0) )
		{
			printf("These are invalid inputs!/n");
			ncases--;
			continue;
		}
		double area = 1/sqrt(p);
		
		printf("%.3lf/n",area);
	}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: