Python最远足迹

B站影视 港台电影 2025-03-09 20:58 2

摘要:import redef is_valid_coordinate(coord):"""判断坐标是否合法"""# 合法的坐标格式为 (x,y),其中 0 < x < 1000,0 < y < 1000,且不能有前导零pattern = r'^\((\d{1,3}

import redef is_valid_coordinate(coord):"""判断坐标是否合法"""# 合法的坐标格式为 (x,y),其中 0 max_distance:max_distance = distancefarthest_coord = coord# 如果没有合法坐标,返回总部坐标 (0,0)return farthest_coord if farthest_coord else "(0,0)"# 自定义输入input_data = input("请输入记录仪中的数据:")# 获取最远足迹坐标result = get_farthest_footprint(input_data)# 输出结果print("最远足迹坐标:", result)正则表达式匹配

使用 re.findall(r'\(\d+,\d+\)', data) 提取所有可能的坐标格式。

使用 is_valid_coordinate 函数进一步验证坐标是否合法。

合法性检查

坐标格式必须为 (x,y),且 x 和 y 不能有前导零。

x 和 y 的取值范围为 0

距离计算

相对总部的距离计算公式为 x * x + y * y。

遍历所有合法坐标,找到距离最大的坐标。

输出结果

如果没有任何合法坐标,返回总部坐标 (0,0)。

否则,返回最远足迹的坐标。

来源:真的教育

相关推荐