P5708_三角形面积(深基2.习2)
Description
一个三角形的三边长分别是 a、b、c,那么它的面积为 p ( p − a ) ( p − b ) ( p − c ) , 其 中 p = 1 2 ( a + b + c ) 。 \sqrt {p(p-a)(p-b)(p-c)} ,其中 p=\frac{1}{2}(a+b+c)。 p(p−a)(p−b)(p−c),其中p=21(a+b+c)。输入这三个数字,计算三角形的面积,四舍五入精确到 1 位小数。保证能构成三角形,0≤a,b,c≤1000,每个边长输入时不超过2位小数。
Input
输入保证能构成三角形的三条边且每个边长不超过2位小数
Output
四舍五入精确到 1 位小数
Sample Input
3 4 5
Sample Output
6.0
这题直接代公式就好了,注意用double,用float最后一个测试点过不了。
#include <cstdio>
#include <cmath>
using namespace std;
int main(){
double a,b,c,p;
scanf("%lf %lf %lf",&a,&b,&c);
p=1.0/2*(a+b+c);
printf("%.1lf",sqrt(p*(p-a)*(p-b)*(p-c)));
return 0;
}