Dev样例过了,洛谷全WA不知道错哪了

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

AniuKarry @ 2021-12-23 20:23:13

#include <iostream>
#include <cmath>
using namespace std;
float ld(float x1,float x2,float y1,float y2)
{
    float num;
    num=((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2));
    num=sqrt(num);
    return num;
}
int main()
{
    float x1,x2,y1,y2,z1,z2,sum;
    cin>>x1>>x2>>y1>>y2>>z1>>z2;
    sum=ld(x1,x2,y1,y2)+ld(y1,y2,z1,z2)+ld(x1,x2,z1,z2);
    printf("%.2f",sum);
    return 0;
}

by Engulf @ 2021-12-23 20:27:27

精度问题?


by Engulf @ 2021-12-23 20:27:59

try double and not float


by ud2_ @ 2021-12-23 20:33:34

换个正常的变量名应该更容易看出错哪。


by AniuKarry @ 2021-12-23 20:34:11

还是不行


by AniuKarry @ 2021-12-23 20:35:30

@ud2_ 我觉得可能是printf在windows和linux下的差异但我一般用cout是因为要保留两位小数才用printf


by ud2_ @ 2021-12-23 20:37:06

@13850375639aniu 比如,平面上的坐标哪来的 z


by AniuKarry @ 2021-12-23 20:39:47

@ud2_ 我是一个蒟蒻XXS


by MushR @ 2022-01-02 13:49:11

应该是

float ld(float x1,float x2,float y1,float y2)
{
    float num;
    num=((y1-x1)*(y1-x1))+((y2-x2)*(y2-x2));
    num=sqrt(num);
    return num;
}

换一个变量名应该会方便看一点


by Eleveslaine @ 2022-01-14 19:51:39

  1. 用 double
  2. x1,x2等变量尽量不要用

by Eleveslaine @ 2022-01-14 19:55:40

等等不对,改过来还是全wa


|