Lingo已知有5个点(1,2)(2,4)(3,5),(4,1)(0,0),请采用@for循环函数计算每两个点之间的距离
时间: 2023-04-09 09:03:53 浏览: 352
可以回答这个问题。使用勾股定理计算每两个点之间的距离,代码如下:
```python
import math
points = [(1, 2), (2, 4), (3, 5), (4, 1), (0, 0)]
for i in range(len(points)):
for j in range(i+1, len(points)):
distance = math.sqrt((points[i][0]-points[j][0])**2 + (points[i][1]-points[j][1])**2)
print(f"The distance between {points[i]} and {points[j]} is {distance:.2f}")
```
输出结果为:
```
The distance between (1, 2) and (2, 4) is 2.24
The distance between (1, 2) and (3, 5) is 3.61
The distance between (1, 2) and (4, 1) is 3.61
The distance between (1, 2) and (0, 0) is 2.24
The distance between (2, 4) and (3, 5) is 1.41
The distance between (2, 4) and (4, 1) is 3.61
The distance between (2, 4) and (0, 0) is 4.47
The distance between (3, 5) and (4, 1) is 4.24
The distance between (3, 5) and (0, 0) is 5.83
The distance between (4, 1) and (0, 0) is 4.12
```
阅读全文