/*
题目描述:
给定平面上任意三个点的坐标(x1,y1)、(x2,y2)、(x3,y3),检验它们能否构成三角形。
输入格式:
输入在一行中顺序给出6个[-100, 100]范围内的数字,即3个点的坐标x1, y1, x2, y2, x3, y3。
输出格式:
若这3个点不能构成三角形,则在一行中输出“Impossible”;若可以,则在一行中输出该三角形的周长和面积,格式为“L = 周长, A = 面积”,输出到小数点后2位。
输入样例1:4 5 6 9 7 8输出样例1:L = 10.13, A = 3.00输入样例2:4 6 8 12 12 18输出样例2:Impossible*/
/*
下面是我写的Java解法,比较麻烦,但是正确的。
看了网上的方法,发现更简单的做法是利用三角形的面积公式:S*S=p*(p-a)*(p-b)*(p-c),其中p=(a+b+c)/2.
*/
import java.util.*; import java.lang.*; import java.text.*; public class Main { class Point{ double x=0.0,y=0.0; } public Point createPoint(){ return new Point(); } public double CalculateArea(Point point1, Point point2, Point point3){ double area = 0.0; double area1 = (point1.y+point2.y)*Math.abs(point2.x-point1.x)/2.0; double area2 = (point3.y+point2.y)*Math.abs(point2.x-point3.x)/2.0; double area3 = (point1.y+point3.y)*Math.abs(point3.x-point1.x)/2.0; area = Math.abs(area1+area2-area3); return area; } public static void main(String[] args){ Main obj = new Main(); Scanner in = new Scanner(System.in); Main.Point point1 = obj.createPoint(); Main.Point point2 = obj.createPoint(); Main.Point point3 = obj.createPoint(); point1.x = in.nextDouble(); point1.y = in.nextDouble(); point2.x = in.nextDouble(); point2.y = in.nextDouble(); point3.x = in.nextDouble(); point3.y = in.nextDouble(); double d1 = (point1.x-point2.x)*(point1.x-point2.x)+(point1.y-point2.y)*(point1.y-point2.y); double d2 = (point1.x-point3.x)*(point1.x-point3.x)+(point1.y-point3.y)*(point1.y-point3.y); double d3 = (point2.x-point3.x)*(point2.x-point3.x)+(point2.y-point3.y)*(point2.y-point3.y); if(Math.sqrt(d1)+Math.sqrt(d2)>Math.sqrt(d3) && Math.sqrt(d1)+Math.sqrt(d3)>Math.sqrt(d2) && Math.sqrt(d2)+Math.sqrt(d3)>Math.sqrt(d1)){ DecimalFormat df = new DecimalFormat("#0.00"); double L = Math.sqrt(d1)+Math.sqrt(d2)+Math.sqrt(d3); System.out.print("L = "+df.format(L)+", "); double[] arrX = {point1.x,point2.x,point3.x}; double[] arrY = {point1.y,point2.y,point3.y}; Arrays.sort(arrX); Arrays.sort(arrY); Main.Point zero = obj.createPoint(); zero.x = arrX[0]; zero.y = arrY[0]; point1.x = point1.x - zero.x; point1.y = point1.y - zero.y; point2.x = point2.x - zero.x; point2.y = point2.y - zero.y; point3.x = point3.x - zero.x; point3.y = point3.y - zero.y; double area = 0.0; if((point1.x-point2.x)*(point1.x-point3.x)<=0){ area = obj.CalculateArea(point2, point1, point3); }else if((point2.x-point1.x)*(point2.x-point3.x)<=0){ area = obj.CalculateArea(point1, point2, point3); }else{ area = obj.CalculateArea(point1, point3, point2); } System.out.print("A = "+df.format(area)+"\n"); }else{ System.out.println("Impossible"); } } }