C语言:习题3-5 三角形判断.2021-07-25

习题3-5 三角形判断 (15 point(s))

给定平面上任意三个点的坐标(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

参考答案:

判定条件1:两边之和大于第三边

判定条件2:面积不为0

判定条件3:三线斜率不相等

判定条件4:两边之和不等于第三边

(判定条件满足一个即可确定为三角形)

答案1

#include<stdio.h>
#include<math.h>

int main()
{
    double perimeter,area,a,b,c;
    int arguments_triangle(double a,double b,double c,double *perimeter,double *area);
    //两点间距离AB的平方为 AB²=(x1-x2)²+(y1-y2)²
    double x1,y1,x2,y2,x3,y3;
    scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3);
	//scanf("%lf")支持接收long int和double
    
    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));
    
    if(arguments_triangle(a,b,c,&perimeter,&area)) printf("L = %.2f, A = %.2f",perimeter,area);
    else puts("Impossible");
}

int arguments_triangle(double a,double b,double c,double *perimeter,double *area)
{
    int triangle(double a,double b,double c,double *perimeter,double *area);
    int code=0;//设置三角形条件状态码,0为不成立,1为成立
    
    //任意两边之和大于第三边
    if(a<b+c) if(b<a+c) if(c<a+b) code=1;
    
    if(0==triangle(a,b,c,perimeter,area)) code=0;
    
    return code;
}

int triangle(double a,double b,double c,double *perimeter,double *area)
{
    int code=1;
    double half_perimeter;
    *perimeter=a+b+c;
    
    //海伦公式
    half_perimeter=*perimeter/2;
    *area=sqrt(half_perimeter*(half_perimeter-a)*(half_perimeter-b)*(half_perimeter-c));
    
    if (0==*area) code=0;//平面坐标三点同线,面积为0
    return code;

}

答案2(精简)

既然面积一定要求,那就用面积来判断,去除部分冗余后

#include<stdio.h>
#include<math.h>

int main()
{
    double x1,y1,x2,y2,x3,y3,perimeter,half_perimeter,area,a,b,c;
    scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3);
	//scanf("%lf")支持接收long int和double
    //两点间距离AB的平方为 AB²=(x1-x2)²+(y1-y2)²
    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));
    perimeter=a+b+c;
    
    //海伦公式
    half_perimeter=perimeter/2;
    if(area=sqrt(half_perimeter*(half_perimeter-a)*(half_perimeter-b)*(half_perimeter-c)))
    printf("L = %.2f, A = %.2f",perimeter,area);
    else puts("Impossible");
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mklpo147

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值