C全wa,下载了第一个测试点,试出来就是答案的6.06,求解!

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

Naitsy @ 2022-11-07 13:17:31

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

double distance(double ,double ,double ,double);//求两个点之间的距离

int main(){
    double a1,a2,a3,b1,b2,b3;
    scanf("%lf %lf",&a1,&b1);
    scanf("%lf %lf",&a2,&b2);
    scanf("%lf %lf",&a3,&b3);

    double a = distance(a1,b1,a2,b2);
    double b = distance(a2,b2,a3,b3);
    double c = distance(a1,b1,a3,b3);

    printf("%.2lf\n",a+b+c);

    return 0;
}

double distance(double a1,double b1,double a2,double b2){
    double dis;
    dis = sqrt(abs(a2-a1)*abs(a2-a1)+abs(b2-b1)*abs(b2-b1));
    return dis;
}

by AC_CSP @ 2022-11-07 13:24:57

dis = sqrt(abs(a2-a1)abs(a2-a1)+abs(b2-b1)abs(b2-b1));

请用fabs.


by Kinzo @ 2022-11-07 13:38:30

我看不懂C语言,你照着我的C++对一下

//Luogu P5735 [Basic 7. Example 1] Distance function

#include<bits/stdc++.h>//include

using namespace std;//initialization

//Define variables
double x1,x2,x3;
double y1q,y2q,y3q;
double answer;

double ftdot (double x1,double x2,double y1,double y2){ //Outlay function
    double dis=0;
    dis=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
    return dis;//Return Results 
}

int main() {    //Principal function
    scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1q,&x2,&y2q,&x3,&y3q);//input

    answer=ftdot(x1,x2,y1q,y2q)+ftdot(x2,x3,y2q,y3q)+ftdot(x1,x3,y1q,y3q); 

    printf("%.2lf",answer);//output

    return 0;//Good habits, great! 
}

by Kinzo @ 2022-11-07 13:40:39

没用y1这样的变量是因为万能头里有一个叫y1的内置函数


by Naitsy @ 2022-11-08 18:21:47

@AC_CSP 谢谢!通过了


|