求助一下,c

P5735 【深基7.例1】距离函数

Mr_ZLH @ 2020-11-03 17:54:22

#include <stdio.h>
#include <math.h>
double dis(double a,double b,double c,double d)
{
    int l;
    l=sqrt((c-a)*(c-a)+(d-b)*(d-b));
    return (l);

}
int main()
{
    double dis(double a,double b,double c,double d);
    double x1,y1,x2,y2,x3,y3;
    double i;
    scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3);
    i=dis(x1,y1,x2,y2)+dis(x2,y2,x3,y3)+dis(x1,y1,x3,y3);
    printf("%.2lf",i);
    return 0;

}

by ud2_ @ 2020-11-03 18:10:20

 #include <stdio.h>
 #include <math.h>
 double dis(double a,double b,double c,double d)
 {
-    int l;
-    l=sqrt((c-a)*(c-a)+(d-b)*(d-b));
-    return (l);
-
+    return hypot(c - a, d - b);
 }
 int main()
 {
-    double dis(double a,double b,double c,double d);
     double x1,y1,x2,y2,x3,y3;
     double i;
     scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3);
     i=dis(x1,y1,x2,y2)+dis(x2,y2,x3,y3)+dis(x1,y1,x3,y3);
     printf("%.2lf",i);
     return 0;
-
 }

by Cander花朵 @ 2020-11-03 18:16:22

double dis\ 结果返回了个int?


by Mr_ZLH @ 2020-11-03 22:34:58

@sjx233_ 感谢大佬,过了。


|