PTA习题3-5 三角形判断 (15分)

给定平面上任意三个点的坐标(x​1​​,y​1​​)、(x​2​​,y​2​​)、(x​3​​,y​3​​),检验它们能否构成三角形。

输入格式:

输入在一行中顺序给出六个[−100,100]范围内的数字,即三个点的坐标x​1​​、y​1​​、x​2​​、y​2​​、x​3​​、y​3​​。

输出格式:

若这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 x1x2,x1x3,x2x3;
    scanf("%lf %lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3);
    if((y2-y1)/(x2-x1)==(y3-y1)/(x3-x1)){
        printf("Impossible");
    }else {
            x1x2=sqrt(pow(y2-y1,2)+pow(x2-x1,2));
            x1x3=sqrt(pow(y3-y1,2)+pow(x3-x1,2));
            x2x3=sqrt(pow(y3-y2,2)+pow(x3-x2,2));
            L=x1x2+x1x3+x2x3;
            A=sqrt(L/2.0*(L/2.0-x1x2)*(L/2.0-x1x3)*(L/2.0-x2x3));
            printf("L = %.2f, A = %.2f",L,A);
    }
    return 0;
}
发布了52 篇原创文章 · 获赞 0 · 访问量 1245

猜你喜欢

转载自blog.csdn.net/qq_38501880/article/details/104934317