写程序输入两个点的坐标 x1、y1、x2、y2,然后输出它们之间的距离
时间: 2024-05-13 22:14:43 浏览: 12
可以使用数学公式计算两个点之间的距离。假设两个点的坐标分别为 (x1, y1) 和 (x2, y2),则它们之间的距离为:
d = √((x2 - x1)² + (y2 - y1)²)
其中,"√" 表示开方。
下面是 Python 代码实现:
```python
import math
x1 = float(input('请输入第一个点的 x 坐标:'))
y1 = float(input('请输入第一个点的 y 坐标:'))
x2 = float(input('请输入第二个点的 x 坐标:'))
y2 = float(input('请输入第二个点的 y 坐标:'))
distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
print('两个点之间的距离为:', distance)
```
在程序中,我们使用了 math 模块的 sqrt() 函数来计算平方根。输入时需要注意输入的是浮点数,输出时可以使用 print() 函数进行输出。
相关问题
要求从键盘输入两个点的坐标 x1、y1、x2、y2,然后输出它们之间的距离的C语言
以下是计算两个点之间距离的C语言程序:
```c
#include <stdio.h>
#include <math.h>
int main() {
double x1, y1, x2, y2, distance;
printf("Enter the coordinates of the first point (x1, y1): ");
scanf("%lf %lf", &x1, &y1);
printf("Enter the coordinates of the second point (x2, y2): ");
scanf("%lf %lf", &x2, &y2);
distance = sqrt(pow((x2-x1), 2) + pow((y2-y1), 2));
printf("The distance between the two points is: %lf\n", distance);
return 0;
}
```
程序要求用户输入两个点的坐标,然后使用数学库函数`sqrt()`和`pow()`计算两个点之间的距离,并输出结果。
输入两个点的坐标(x1,y1)、(x2,y2),编写一个计算这两点之间距离的C语言程序
以下是计算两点之间距离的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", distance);
return 0;
}
```
运行程序后,先输入第一个点的坐标,再输入第二个点的坐标,程序会输出这两点之间的距离。注意输入坐标时,用逗号隔开。