要求从键盘输入两个点的坐标x1、y1、x2、y2,然后输出它们之间的距离。输入格式: 在一行中输入4个整数,用空格分开。 输出格式: 输出计算结果,2位小数,用c语言写出代码
时间: 2023-05-25 11:06:25 浏览: 129
```
#include<stdio.h>
#include<math.h> //使用sqrt函数需要引入math.h头文件
int main()
{
int x1, y1, x2, y2;
double distance; //距离用浮点型表示
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
distance = sqrt((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1)); //计算两个点之间的距离
printf("%.2f", distance); //输出距离,保留两位小数
return 0;
}
```
相关问题
要求从键盘输入两个点的坐标 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,然后输出它们之间的距离
可以使用数学公式计算两个点之间的距离。假设两个点的坐标分别为 (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() 函数进行输出。