首页 > 试题广场 >

三角形判断:输入平面上任意三个点的坐标(x1,y1)、(x2

[问答题]

三角形判断:输入平面上任意三个点的坐标(x1,y1)、(x2,y2)、(x3,y3),检验它们能否构成三角形。如果这3个点能构成一个三角形,输出周长和面积(保留两位小数)否则,输出 “Impossible”试编写相应程序。

推荐


#include<stdio.h> 
#include<math.h> 
//#include<iostream> 
#include<stdlib.h> 
//using namespace std; 
int main() 
{double x1,y1,x2,y2,x3,y3; 
scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3); 
double one,two,three; 
one=sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); 
two=sqrt((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1)); 
three=sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2)); 
double l,a; 
if((one+two)>three&&(one+three)>two&&(two+three)>one) 
{l=one+two+three; 
a=sqrt(0.5*l*(0.5*l-one)*(0.5*l-two)*(0.5*l-three)); 
printf("L = %.2f, A = %.2f\n",l,a);} 
else printf("Impossible\n"); 
return 0; 
}


发表于 2018-05-06 21:21:52 回复(0)
/*
三角形判断 
author: a1bum 
*/

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

int main(){
    double x[3],y[3],v[3];
    int i;

    for( i = 0; i < 3; i++ ){
        printf("please input point%d: ", i+1);
        scanf("%lf%lf", &x[i], &y[i]);
    }
    // 求两点之间距离 
    v[0] = sqrt(pow(x[1]-x[0],2)+pow(y[1]-y[0],2));
    v[1] = sqrt(pow(x[2]-x[1],2)+pow(y[2]-y[1],2));
    v[2] = sqrt(pow(x[2]-x[0],2)+pow(y[2]-y[0],2));
//    printf("%.lf %.lf %.lf", v[0],v[1],v[2]);
    if( v[0]+v[1]>v[2] && v[0]+v[2]>v[1] && v[1]+v[2]>v[0]){
        double l =  v[0]+v[1]+v[2];
        printf("circle: %.2lf", l);
        double p = l/2;
        printf("area: %.2lf", sqrt(p*(p-v[0])*(p-v[2])*(p-v[3])) );        // 利用海伦-秦九韶公式 
    }else{
        printf("Impossible");
    } 

    return 0;
}

谢谢楼上林呼哥的参考

编辑于 2018-06-05 23:59:55 回复(0)
/*
三角形的判断
*/

#include<stdio.h>
#include<math.h>
int main()
{
    double x1,x2,x3,y1,y2,y3,a,b,c,s,p;

    scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3);
    //求三条边a,b,c.
    a=sqrt(pow(x1-x2,2)+pow(y1-y2,2));
    b=sqrt(pow(x1-x3,2)+pow(y1-y3,2));
    c=sqrt(pow(x3-x2,2)+pow(y3-y2,2));
    if(a+b>c&&a+c>b&&c+b>a)
    {
        p=(a+b+c)/2;
        s=sqrt(p*(p-a)*(p-b)*(p-c));
        printf("周长: %.1f面积: %.1f",p*2,s);
    }
    else
        printf("Impossible");
    return 0;
}






发表于 2021-02-19 20:29:53 回复(0)