UVa 10347

From Algorithmist
Jump to navigation Jump to search

10347 - Medians[edit]

Summary[edit]

Given the lenghts of all three medians in a triangle, compute that triangle's area.

Explanation[edit]

This is a very easy problem. All you need to know is the correct formula, that's it. You may check out this. There is a trick. Output of your program must be satisfy for all the inputs, including the Negative inputs. Examples: For the Medians (2, 3, 4) the area of the triangle is 3.873, this is also the output for the Medians (-2,-3,-4).That means, negative values must be considered here.


Don't see the UVA-toolkit outputs. Outputs of this problem in UVA-toolkit is wrong. (Correction from SEC(Swordfish))

Input[edit]

3 3 3

Output[edit]

5.196

Implementations[edit]

Implementation in Java[edit]

import java.util.Scanner;
import java.util.StringTokenizer;

public class UVA_10347 {

	public static void main(String[] args) {
		Scanner sc =new Scanner(System.in);
		while(sc.hasNextLine())
		{
			String line =sc.nextLine();
			if(line == null || line.isEmpty())
			{
				break;
			}
			StringTokenizer st =new StringTokenizer(line);
			double m1= Double.parseDouble(st.nextToken());
			double m2 =Double.parseDouble(st.nextToken());
			double m3 = Double.parseDouble(st.nextToken());
			double sm =(m1+m2+m3)/2.0;
			double a_sq = sm*(sm-m1)*(sm-m2)*(sm-m3);
			if(a_sq<=0)
			{
				double f =-1.0;
				System.out.printf("%.3f\n",f);
			}
			else
			{
				double s =Math.sqrt(a_sq)*(4/3.0);
				System.out.printf("%.3f\n",s);
			}
		}
	}

}

References[edit]

  1. Area of a Triangle Given the Medians