用C语言写平面上两点的距离
时间: 2023-04-04 08:00:55 浏览: 83
可以使用以下代码来计算平面上两点的距离:
#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("两点之间的距离为: %f\n", distance);
return ;
}
相关问题
C语言 平面两点两点距离代码
以下是计算平面两点距离的 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 ;
}
希望能对您有所帮助。
计算平面上两点间的距离c语言中午
可以使用以下公式来计算平面上两点之间的距离:
d = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
其中,(x1, y1)和(x2, y2)是两个点的坐标,d是这两个点之间的距离,sqrt()是计算平方根的函数。
以下是一个计算两点距离的C语言代码示例:
```c
#include <stdio.h>
#include <math.h>
int main() {
double x1, y1, x2, y2, distance;
printf("Enter the coordinates of point 1: ");
scanf("%lf %lf", &x1, &y1);
printf("Enter the coordinates of point 2: ");
scanf("%lf %lf", &x2, &y2);
distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
printf("The distance between the two points is: %lf\n", distance);
return 0;
}
```
在上面的代码中,我们首先使用scanf()函数从用户那里获取两个点的坐标,然后计算它们之间的距离,并将结果输出到控制台。