[UVA][math] 10522 - Height to Area
THE SAMS' CONTEST
Problem 3
Height to Area |
Problem
It's an easy geometry problem. For any triangle ABC we know that the height from A to the line BC (or it's extension) is Ha, from B to the line AC (or it's extension) is Hb and from C to the line AB (or it's extension) is Hc. Now you are given these three values and you have to figure out the area of the ABC .
Input
At first the input will be an integer n. Which denotes the number of invalid inputs after which the input will terminate. Then there will be three real numbers Ha, Hb and Hc per line.
Output
For each input block there should be one output line. For valid inputs the line contains the area of the ABC up to 3 decimal places after the decimal point and for invalid inputs there will be a line "These are invalid inputs!". After n invalid input sets the program will terminate.
Sample Input
1
31.573 22.352 63.448
46.300 50.868 86.683
22.005 24.725
22.914
5.710 25.635 32.805
Sample Output
1517.456
2219.941
311.804
These are invalid inputs!
Enamul Haque
Triangle is a very fine geometrical shape,
you can't say this is bad;
But if you familiar with "Triangle Love",
you must know that will make you sad.
--- Moni.
則由於面積相同
A(area) = 0.5a*Ha = 0.5b*Hb = 0.5c*Hc
接著利用海龍公式 Heron's formula
s = (a+b+c)/2
A = sqrt((s)*(s-a)*(s-b)*(s-c))
將 s 中的 a, b, c 替換,
最後得到
A = A*A * sqrt((1/Ha+1/Hb+1/Hc)*(1/Hb+1/Hc-1/Ha)*(1/Ha+1/Hc-1/Hb)*(1/Hb+1/Ha-1/Hc))
A = 1/sqrt((1/Ha+1/Hb+1/Hc)*(1/Hb+1/Hc-1/Ha)*(1/Ha+1/Hc-1/Hb)*(1/Hb+1/Ha-1/Hc));
#include <stdio.h>
#include <math.h>
int main() {
int cases = 0;
scanf("%d", &cases);
while(cases) {
double Ha, Hb, Hc;
scanf("%lf %lf %lf", &Ha, &Hb, &Hc);
double val;
val = (1/Ha+1/Hb+1/Hc)*(1/Hb+1/Hc-1/Ha)*(1/Ha+1/Hc-1/Hb)*(1/Hb+1/Ha-1/Hc);
#define eps 1e-8
if(val < eps || fabs(Ha) < eps || fabs(Hb) < eps || fabs(Hc) < eps) {
puts("These are invalid inputs!");
cases--;
continue;
}
printf("%.3lf\n", 1/sqrt(val));
}
return 0;
}
/*
A(area) = 0.5a*Ha = 0.5b*Hb = 0.5c*Hc
Heron's formula
s = (a+b+c)/2
A = sqrt((s)*(s-a)*(s-b)*(s-c))
A = A*A * sqrt((1/Ha+1/Hb+1/Hc)*(1/Hb+1/Hc-1/Ha)*(1/Ha+1/Hc-1/Hb)*(1/Hb+1/Ha-1/Hc))
A = 1/sqrt((1/Ha+1/Hb+1/Hc)*(1/Hb+1/Hc-1/Ha)*(1/Ha+1/Hc-1/Hb)*(1/Hb+1/Ha-1/Hc));
*/