您的位置:首页 > 其它

UVa10522 - Height to Area(海伦公式)

2014-07-26 18:42 323 查看

THE SAMS' CONTEST

Problem 3

Height to Area 

Problem

    It's an easygeometry problem. For any triangle ABC weknow that the height from A to the line BC (or it's extension) is Ha, from B tothe line AC (or it's extension) is Hb and from C to the line AB (or it'sextension)
is Hc. Now you are given these three values and you have to figureout the area of the ABC.

                              


Input

    At first the input will be an integer n. Which denotes thenumber of invalid inputs after which the input will terminate. Then there willbe three real numbers Ha, Hb and Hc per line.

Output

    For each input block there should be one output line. Forvalid inputs the line contains the area of the ABCup to 3 decimal places after the decimal point and for invalid inputs there willbe a line "These are invalid inputs!". After n invalid input sets
the programwill terminate.

Sample Input

1

31.573 22.352 63.448

46.300 50.868 86.683

22.005 24.72522.914

5.710 25.635 32.805

 

Sample Output

1517.456

2219.941

311.804

These are invalid inputs!

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

public class Main {
public static final boolean DEBUG = false;
public static final double EPS = 1e-8;
public StreamTokenizer tokenizer;
public BufferedReader cin;
public PrintWriter cout;
public double ha, hb, hc;
public int t;
public int cnt;

public void init()
{
try {
if (DEBUG) {
cin = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\\OJ\\uva_in.txt")));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}
cout = new PrintWriter(new OutputStreamWriter(System.out));
tokenizer = new StreamTokenizer(cin);
} catch (Exception e) {
e.printStackTrace();
}
}

public String next()
{
try {

tokenizer.nextToken();
if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;
else if (tokenizer.ttype == StreamTokenizer.TT_WORD) return tokenizer.sval;
else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) return String.valueOf(tokenizer.nval);
else return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

public void inputError()
{
t = (int)Double.parseDouble(next());
cnt = 0;
}

public boolean input()
{
ha = Double.parseDouble(next());
hb = Double.parseDouble(next());
hc = Double.parseDouble(next());

return true;
}

public boolean solve()
{
if (Math.abs(ha) < EPS || Math.abs(hb) < EPS || Math.abs(hc) < EPS) {
cnt++;
cout.println("These are invalid inputs!");
cout.flush();
if (cnt == t) return false;
else return true;
}

double s = (1 / ha + 1 / hb + 1 / hc) * (1 / ha + 1 / hb - 1 / hc) * (1 / ha + 1 / hc - 1 / hb) * (1 / hb + 1 / hc - 1 / ha);

if (s < EPS) {
cnt++;
cout.println("These are invalid inputs!");
cout.flush();
if (cnt == t) return false;
else return true;
}

cout.printf("%.3f", 1 / Math.sqrt(s));
cout.println();
cout.flush();
return true;
}

public static void main(String[] args) {
Main solver = new Main();
solver.init();

solver.inputError();
while (true) {
solver.input();
if (!solver.solve()) break;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: