UVA 10522 已知三高求三角型面积

题意:

已知三高求三角型面积

 

题解:
没想到还要用到海伦公式。。。。

果断看题解。。。

设对应三边为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就过了。

 

View Code
 1 #include <queue>
 2 #include <stack>
 3 #include <math.h>
 4 #include <stdio.h>
 5 #include <stdlib.h>
 6 #include <iostream>
 7 #include <limits.h>
 8 #include <string.h>
 9 #include <string>
10 #include <algorithm>
11 using namespace std;
12 const double eps = 1e-8;
13 bool dy(double x,double y)    {    return x > y + eps;}    // x > y 
14 bool xy(double x,double y)    {    return x < y - eps;}    // x < y 
15 bool dyd(double x,double y)    {     return x > y - eps;}    // x >= y 
16 bool xyd(double x,double y)    {    return x < y + eps;}     // x <= y 
17 bool dd(double x,double y)     {    return fabs( x - y ) < eps;}  // x == y
18 int main()
19 {
20     int ncases;
21     double x,y,z;
22     
23     scanf("%d",&ncases);
24     
25     while( ncases )
26     {
27         scanf("%lf%lf%lf",&x,&y,&z);
28         if( dd(x,0.0) || dd(y,0.0) || dd(z,0.0) )
29         {
30             printf("These are invalid inputs!/n");
31             ncases--;
32             continue;
33         }
34         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);
35         if( xyd(p,0.0) )
36         {
37             printf("These are invalid inputs!/n");
38             ncases--;
39             continue;
40         }
41         double area = 1/sqrt(p);
42         
43         printf("%.3lf/n",area);
44     }
45 return 0;
46 }

转载自:http://blog.csdn.net/zxy_snow/article/details/6568417

PS:小媛姐姐(请允许我这样叫)的博客真心好~没事去看看吧~

http://blog.csdn.net/zxy_snow

 

 

posted @ 2013-02-25 22:17  proverbs  阅读(257)  评论(0编辑  收藏  举报