求两点间的距离c语言
时间: 2023-12-11 19:04:09 浏览: 106
可以使用以下代码来计算两点间的距离(欧几里德距离):
```c
#include <stdio.h>
#include <math.h>
int main() {
double x1, y1, x2, y2, distance;
printf("请输入第一个点的坐标(x1, y1): ");
scanf("%lf %lf", &x1, &y1);
printf("请输入第二个点的坐标(x2, y2): ");
scanf("%lf %lf", &x2, &y2);
distance = sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2));
printf("两点间的距离为: %.2lf\n", distance);
return 0;
}
```
相关问题
c语言求两点间的距离
两点间的距离可以用勾股定理求解。设两点分别为 (x1, y1) 和 (x2, y2),则它们之间的距离为:
d = sqrt((x2-x1)^2 + (y2-y1)^2)
其中 sqrt 表示求平方根。因此,C语言代码如下:
#include <stdio.h>
#include <math.h>
int main() {
double x1, y1, x2, y2, distance;
printf("请输入第一个点的坐标(x1, y1): ");
scanf("%lf %lf", &x1, &y1);
printf("请输入第二个点的坐标(x2, y2): ");
scanf("%lf %lf", &x2, &y2);
distance = sqrt(pow(x2-x1, 2) + pow(y2-y1, 2));
printf("两点之间的距离为: %lf\n", distance);
return 0;
}
求两点距离的C语言代码
以下是求两点距离的C语言代码:
#include <stdio.h>
#include <math.h>
int main()
{
float x1, y1, x2, y2, distance;
printf("请输入第一个点的坐标(x1, y1):");
scanf("%f %f", &x1, &y1);
printf("请输入第二个点的坐标(x2, y2):");
scanf("%f %f", &x2, &y2);
distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
printf("两点之间的距离为:%.2f\n", distance);
return 0;
}