习题5-3 使用函数计算两点间的距离(10 分)

习题5-3 使用函数计算两点间的距离(10 分)

本题要求实现一个函数,对给定平面任意两点坐标$(x_1,y_1)$和$(x_2,y_2)$,求这两点之间的距离。

函数接口定义:

1
double dist( double x1, double y1, double x2, double y2 );

其中用户传入的参数为平面上两个点的坐标$(x_1, y_1)$和$(x_2, y_2)$,函数dist应返回两点间的距离。

裁判测试程序样例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <math.h>

double dist( double x1, double y1, double x2, double y2 );

int main()
{
double x1, y1, x2, y2;

scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
printf("dist = %.2f\n", dist(x1, y1, x2, y2));

return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

1
10 10 200 100

输出样例:

1
dist = 210.24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <math.h>

double dist( double x1, double y1, double x2, double y2 );

int main()
{
double x1, y1, x2, y2;

scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
printf("dist = %.2f\n", dist(x1, y1, x2, y2));

return 0;
}

double dist( double x1, double y1, double x2, double y2 ){
double f;
f=sqrt(pow(x2-x1,2)+pow(y2-y1,2));
return f;
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×