想问问这个代码有什么问题,不管输什么数据都只会得到1.41这个答案
#include<stdio.h>
#include<math.h>
float dist(float *x1,float *y1,float *x2,float *y2)
{
float rs;
scanf("%f%f%f%f",x1,y1,x2,y2);
rs=sqrt(pow(x1-x2,2)+pow(y1-y2,2));
return rs;
}
int main()
{
float rs,x1,x2,y1,y2;
rs=dist(&x1,&y1,&x2,&y2);
printf("%.2f",rs);
return 0;
}
看一下你的数据是不是代错了
求两点距离一般是将对应维度的坐标差的平方相加,然后开根号
把你的代码用代码段</>的文本形式发一下,我调试下看看,你发图片我们没办法调试和修改。
你题目的解答代码如下:
#include <stdio.h>
#include <math.h>
float dist(float *x1, float *y1, float *x2, float *y2)
{
float rs;
scanf("%f%f%f%f", x1, y1, x2, y2);
rs = sqrt(pow(*x1 - *x2, 2) + pow(*y1 - *y2, 2)); //dist函数中x1,x2,y1,y2是指针,取值时要在前面加*
return rs;
}
int main()
{
float rs, x1, x2, y1, y2;
rs = dist(&x1, &y1, &x2, &y2);
printf("%.2f", rs);
return 0;
}
如有帮助,望采纳!谢谢!