习题5-3 使用函数计算两点间的距离
本题要求实现一个函数,对给定平面任意两点坐标(x1,y1)和(x2,y2),求这两点之间的距离。
函数接口定义:
double dist( double x1, double y1, double x2, double y2 );
其中用户传入的参数为平面上两个点的坐标(x1
, y1
)和(x2
, y2
),函数dist
应返回两点间的距离。
裁判测试程序样例:
#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;
}
/* 你的代码将被嵌在这里 */
输入样例:
10 10 200 100
输出样例:
dist = 210.24
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
double dist(double x1,double y1,double x2,double y2);
int main(int argc, char *argv[]) {
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){
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}