求助!为什么本地答案正确,线上的全红了

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

nbswsinwt @ 2024-09-02 22:24:19


#include<iostream>
#include<math.h>
using namespace std;
class Position
{
public:
    int x;
    int y;
};
int Distance(Position a, Position b )
{
    int dis = 0;
    dis = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
    dis = sqrt(dis);
    return dis;
}

int main()
{
    Position a, b, c;
    cin >> a.x>>a.y;
    cin >> b.x >> b.y;
    cin >> c.x >> c.y;
    int dis1 = Distance(a, b);
    int dis2 = Distance(a, c);
    int dis3 = Distance(b, c);
    float Dis=(float)( dis1 + dis2 + dis3);
    printf("%.2f", Dis);
    return 0;
}

by __LePetitPrince__ @ 2024-09-02 22:26:18

@nbswsinwt 求出来的距离用 float 存


by lxr_Galaxy @ 2024-09-02 22:29:01

int类型会向下取整,建议float @nbswsinwt


by nbswsinwt @ 2024-09-03 18:06:28

@LePetitPrince 收到,感谢指点!


by nbswsinwt @ 2024-09-03 18:06:45

@LePetitPrince 好的好的,感谢指点


|