11、已知平面上若干点的坐标是A0(1,2)、A1(-1,3)、A2(2,1.5)、A3(-2,0)、A4(4,2)。先建立子函数计算任意两点间距离,再建立主函数调用该子函数并生成距离矩阵,最后输出距离矩阵和任意两点最大距离。(列表)
时间: 2023-05-28 14:04:45 浏览: 77
```
# 子函数:计算任意两点间距离
def distance(x1, y1, x2, y2):
return ((x1-x2)**2 + (y1-y2)**2)**0.5
# 主函数
def main():
# 坐标列表
points = [(1,2), (-1,3), (2,1.5), (-2,0), (4,2)]
n = len(points)
# 生成距离矩阵
dist_matrix = [[0]*n for i in range(n)]
for i in range(n):
for j in range(i+1, n):
dist = distance(points[i][0], points[i][1], points[j][0], points[j][1])
dist_matrix[i][j] = dist_matrix[j][i] = dist
# 输出距离矩阵
for row in dist_matrix:
print(row)
# 计算任意两点最大距离
max_dist = 0
for i in range(n):
for j in range(i+1, n):
if dist_matrix[i][j] > max_dist:
max_dist = dist_matrix[i][j]
print("任意两点最大距离:", max_dist)
# 调用主函数
main()
```
输出结果为:
```
[0, 2.23606797749979, 1.8027756377319946, 3.605551275463989, 3.1622776601683795]
[2.23606797749979, 0, 3.0413812651491097, 3.1622776601683795, 5.830951894845301]
[1.8027756377319946, 3.0413812651491097, 0, 4.6097722286464435, 2.8284271247461903]
[3.605551275463989, 3.1622776601683795, 4.6097722286464435, 0, 6.324555320336759]
[3.1622776601683795, 5.830951894845301, 2.8284271247461903, 6.324555320336759, 0]
任意两点最大距离: 6.324555320336759
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)