给定平面上任意三个点的坐标(x1,y1),(x2,y2),(x3,y3)检验它们能否构成三角形。
输入格式:
输入在一行中顺序给出六个[−100,100]范围内的数字,即三个点的坐标(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
# include <stdio.h>
# include <math.h>
int main()
{
double x1,x2,x3,y1,y2,y3;
double L,A;
double len1,len2,len3;
scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3);
len1=sqrt(pow(y2-y1,2)+pow(x2-x1,2));
len2=sqrt(pow(y3-y1,2)+pow(x3-x1,2));
len3=sqrt(pow(y2-y3,2)+pow(x2-x3,2));
if((len1+len2>len3)&&(len1+len3>len2)&&(len2+len3>len1))
{
L=len1+len2+len3;
A=sqrt(0.5*L*(0.5*L-len1)*(0.5*L-len2)*(0.5*L-len3));
printf("L = %.2lf, A = %.2lf",L,A);
}
else
printf("Impossible\n");
return 0;
}
经典两边之和大于第三边判断三角形
plus
海伦凯勒法求三角形的面积在计算机、算法中非常常用