给定平面上任意三个点的坐标(x1 ,y1 )、(x2 ,y2 )、(x3 ,y3 ),检验它们能否构成三角形。
输入在一行中顺序给出六个[−100,100]范围内的数字,即三个点的坐标x1、y1、x2、y2、x3、y3。
若这3个点不能构成三角形,则在一行中输出“Impossible”;若可以,则在一行中输出该三角形的周长和面积,格式为“L = 周长, A = 面积”,输出到小数点后2位。
4 5 6 9 7 8
L = 10.13, A = 3.00
4 6 8 12 12 18
Impossible
//方法一 数组
#include
#include
int main(){
double x[3]={0},y[3]={0};
double a=0.0,b=0.0,c=0.0,p=0.0;
int i;
double L,A;
for(i=0;i<3;i++){
scanf("%lf",&x[i]);
scanf("%lf",&y[i]);
}
a=sqrt(pow(x[0]-x[1],2)+pow(y[0]-y[1],2));//求三边
b=sqrt(pow(x[1]-x[2],2)+pow(y[1]-y[2],2));
c=sqrt(pow(x[2]-x[0],2)+pow(y[2]-y[0],2));
L = a+b+c;
p= 1.0*L/2;
A=sqrt(1.0*p*(p-a)*(p-b)*(p-c)); //海拉公式
if(a+b<=c || a+c<=b || c+b<=a ){ //判断是否为三角形
printf("Impossible\n");
}else{
L*=1.0;
printf("L = %.2f, A = %.2f\n",L,A);
}
return 0;
}
//方法二 常量
#include
#include
int main(){
double x1,y1,x2,y2,x3,y3;
double a=0.0,b=0.0,c=0.0,p=0.0;
int i;
double L,A;
scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3);//求三边
a=sqrt(pow(x1-x2,2)+pow(y1-y2,2));
b=sqrt(pow(x2-x3,2)+pow(y2-y3,2));
c=sqrt(pow(x3-x1,2)+pow(y3-y1,2));
L = a+b+c;
p= 1.0*L/2;
A=sqrt(1.0*p*(p-a)*(p-b)*(p-c));//海拉公式
if(a+b<=c || a+c<=b || c+b<=a ){ //判断是否构成三角形
printf("Impossible\n");
}else{
L*=1.0;
printf("L = %.2f, A = %.2f\n",L,A);
}
return 0;
}