
目录
某探险队负责对地下洞穴进行探险。探险队成员在进行探险任务时,随身携带的记录器会不定期地记录自身的坐标,但在记录的间隙中也会记录其他数据。探索工作结束后,探险队需要获取到某成员在探险过程中相对于探险队总部的最远的足迹位置。
- 仪器记录坐标时,坐标的数据格式为(x,y),如(1,2)、(100,200),其中0
- 设定探险队总部的坐标为(0,0),某位置相对总部的距离为:x*x+y*y。
- 若两个座标的相对总部的距离相同,则第一次到达的坐标为最远的足迹。
- 若记录仪中的坐标都不合法,输出总部坐标(0,0)。
不需要考虑双层括号嵌套的情况,比如sfsdfsd((1,2))。
字符串,表示记录仪中的数据。
如:ferga13fdsf3(100,200)f2r3rfasf(300,400)
字符串,表示最远足迹到达的坐标。
如: (300,400)
| 输入 | ferg(3,10)a13fdsf3(3,4)f2r3rfasf(5,10) |
| 输出 | (5,10) |
| 说明 | 记录仪中的合法坐标有3个: (3,10), (3,4), (5,10),其中(5,10)是相距总部最远的坐标, 输出(5,10)。 |
| 输入 | asfefaweawfaw(0,1)fe |
| 输出 | (0,0) |
| 说明 | 记录仪中的坐标都不合法,输出总部坐标(0,0)。 |
实现这个题目,需要解析字符串中的有效坐标,计算它们到总部 (0,0) 的距离,并找出最远的一个坐标。如果记录中的所有坐标都不合法,我们将返回总部坐标 (0,0)。
实现步骤如下:
- 解析字符串:提取出所有合法的坐标。
- 验证坐标合法性:确保坐标满足格式要求,且 x 和 y 在 (0,1000) 之间。
- 计算距离:计算每个合法坐标到总部 (0,0) 的距离。
- 找出最远的坐标:在所有合法坐标中找出距离最大的那个。
- 处理异常情况:如果没有合法坐标,返回总部坐标 (0,0)。
下面是具体实现代码:
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- public class CaveExploration {
- public static void main(String[] args) {
- String input = "ferga13fdsf3(100,200)f2r3rfasf(300,400)";
- System.out.println(findFarthestCoordinate(input));
- }
-
- public static String findFarthestCoordinate(String input) {
- // 定义正则表达式以提取合法的坐标
- Pattern pattern = Pattern.compile("\\((\\d{1,3}),(\\d{1,3})\\)");
- Matcher matcher = pattern.matcher(input);
-
- String farthestCoordinate = "(0,0)";
- int maxDistance = 0;
-
- while (matcher.find()) {
- String xStr = matcher.group(1);
- String yStr = matcher.group(2);
- int x = Integer.parseInt(xStr);
- int y = Integer.parseInt(yStr);
-
- // 检查坐标是否合法
- if (isValidCoordinate(xStr, yStr, x, y)) {
- int distance = x * x + y * y;
- if (distance > maxDistance) {
- maxDistance = distance;
- farthestCoordinate = "(" + x + "," + y + ")";
- }
- }
- }
-
- return farthestCoordinate;
- }
-
- // 验证坐标是否合法
- private static boolean isValidCoordinate(String xStr, String yStr, int x, int y) {
- return x > 0 && x < 1000 && y > 0 && y < 1000 &&
- !xStr.startsWith("0") && !yStr.startsWith("0");
- }
- }
在Java中,我们使用正则表达式提取字符串中的坐标。
- Pattern pattern = Pattern.compile("\\((\\d{1,3}),(\\d{1,3})\\)");
- Matcher matcher = pattern.matcher(input);
解析思路:
\\((\\d{1,3}),(\\d{1,3})\\) 匹配形如 (x,y) 的坐标,其中 x 和 y 是 1 到 3 位的数字。matcher.find() 用于在输入字符串中查找所有符合正则表达式的子字符串。通过解析得到的字符串,进一步验证它们是否合法。
- private static boolean isValidCoordinate(String xStr, String yStr, int x, int y) {
- return x > 0 && x < 1000 && y > 0 && y < 1000 &&
- !xStr.startsWith("0") && !yStr.startsWith("0");
- }
验证思路:
计算每个合法坐标到总部 (0,0) 的距离。
通过这种方式,可以确保找出距离总部最远的合法坐标,并处理非法坐标和无坐标的情况。
使用C语言实现这个题目,我们需要解析字符串中的有效坐标,计算它们到总部 (0,0) 的距离,并找出最远的一个坐标。如果记录中的所有坐标都不合法,我们将返回总部坐标 (0,0)。
- #include
- #include
- #include
- #include
-
- // 判断是否是合法的坐标
- int isValidCoordinate(char *xStr, char *yStr, int x, int y) {
- return x > 0 && x < 1000 && y > 0 && y < 1000 &&
- (xStr[0] != '0' || strlen(xStr) == 1) &&
- (yStr[0] != '0' || strlen(yStr) == 1);
- }
-
- int main() {
- char input[] = "ferga13fdsf3(100,200)f2r3rfasf(300,400)";
- char *ptr = input;
- char xStr[4], yStr[4];
- int maxDistance = 0;
- char farthestCoordinate[10] = "(0,0)";
-
- while ((ptr = strstr(ptr, "(")) != NULL) {
- if (sscanf(ptr, "(%3[0-9],%3[0-9])", xStr, yStr) == 2) {
- int x = atoi(xStr);
- int y = atoi(yStr);
- if (isValidCoordinate(xStr, yStr, x, y)) {
- int distance = x * x + y * y;
- if (distance > maxDistance) {
- maxDistance = distance;
- sprintf(farthestCoordinate, "(%d,%d)", x, y);
- }
- }
- }
- ptr++;
- }
-
- printf("%s\n", farthestCoordinate);
- return 0;
- }
在C语言中,我们使用 strstr 和 sscanf 来解析字符串。
- while ((ptr = strstr(ptr, "(")) != NULL) {
- if (sscanf(ptr, "(%3[0-9],%3[0-9])", xStr, yStr) == 2) {
- ...
- }
- ptr++;
- }
strstr 查找字符串中第一个 ( 的位置。sscanf 提取括号中的数字对,格式为 (%3[0-9],%3[0-9]),确保读取的数字不超过3位。通过解析得到的字符串,进一步验证它们是否合法。
- int isValidCoordinate(char *xStr, char *yStr, int x, int y) {
- return x > 0 && x < 1000 && y > 0 && y < 1000 &&
- (xStr[0] != '0' || strlen(xStr) == 1) &&
- (yStr[0] != '0' || strlen(yStr) == 1);
- }
验证思路:
计算每个合法坐标到总部 (0,0) 的距离。
int distance = x * x + y * y;
计算思路:
distance = x * x + y * y。在所有合法坐标中找出距离最大的那个。
- if (distance > maxDistance) {
- maxDistance = distance;
- sprintf(farthestCoordinate, "(%d,%d)", x, y);
- }
思路:
maxDistance 记录最大距离,每次计算新的距离后与之比较,更新最大距离和最远坐标。使用Python实现这个题目同样需要解析字符串中的有效坐标,计算它们到总部 (0,0) 的距离,并找出最远的一个坐标。如果记录中的所有坐标都不合法,我们将返回总部坐标 (0,0)。
- 解析字符串:提取出所有合法的坐标。
- 验证坐标合法性:确保坐标满足格式要求,且 x 和 y 在 (0,1000) 之间。
- 计算距离:计算每个合法坐标到总部 (0,0) 的距离。
- 找出最远的坐标:在所有合法坐标中找出距离最大的那个。
- 处理异常情况:如果没有合法坐标,返回总部坐标 (0,0)。
- import re
-
- def find_farthest_coordinate(data):
- # 定义正则表达式以提取合法的坐标
- pattern = re.compile(r'\((\d{1,3}),(\d{1,3})\)')
- matches = pattern.findall(data)
-
- max_distance = 0
- farthest_coordinate = "(0,0)"
-
- for match in matches:
- x_str, y_str = match
- x = int(x_str)
- y = int(y_str)
-
- # 检查坐标是否合法
- if is_valid_coordinate(x_str, y_str, x, y):
- distance = x * x + y * y
- if distance > max_distance:
- max_distance = distance
- farthest_coordinate = f"({x},{y})"
-
- return farthest_coordinate
-
- def is_valid_coordinate(x_str, y_str, x, y):
- # 验证坐标是否合法
- return 0 < x < 1000 and 0 < y < 1000 and \
- not (x_str.startswith("0") and len(x_str) > 1) and \
- not (y_str.startswith("0") and len(y_str) > 1)
-
- # 示例输入
- input_data = "ferga13fdsf3(100,200)f2r3rfasf(300,400)"
- print(find_farthest_coordinate(input_data)) # 输出: (300,400)
在Python中,我们使用正则表达式提取字符串中的坐标。
- pattern = re.compile(r'\((\d{1,3}),(\d{1,3})\)')
- matches = pattern.findall(data)
\((\d{1,3}),(\d{1,3})\) 匹配形如 (x,y) 的坐标,其中 x 和 y 是 1 到 3 位的数字。findall 方法返回所有匹配的坐标对。通过解析得到的字符串,进一步验证它们是否合法。
- def is_valid_coordinate(x_str, y_str, x, y):
- return 0 < x < 1000 and 0 < y < 1000 and \
- not (x_str.startswith("0") and len(x_str) > 1) and \
- not (y_str.startswith("0") and len(y_str) > 1)
验证思路:
计算每个合法坐标到总部 (0,0) 的距离。
distance = x * x + y * y
计算思路:
distance = x * x + y * y。在所有合法坐标中找出距离最大的那个。
- if distance > max_distance:
- max_distance = distance
- farthest_coordinate = f"({x},{y})"
思路:
max_distance 记录最大距离,每次计算新的距离后与之比较,更新最大距离和最远坐标。如果没有合法坐标,最远的坐标默认为 (0,0)。
- max_distance = 0
- farthest_coordinate = "(0,0)"
思路:
(0,0),如果找到合法坐标,更新为最远坐标。通过上述步骤,可以在Python中实现解析字符串并找出距离总部最远的合法坐标,并处理非法坐标和无坐标的情况。
使用JavaScript实现这个题目同样需要解析字符串中的有效坐标,计算它们到总部 (0,0) 的距离,并找出最远的一个坐标。如果记录中的所有坐标都不合法,我们将返回总部坐标 (0,0)。
- 解析字符串:提取出所有合法的坐标。
- 验证坐标合法性:确保坐标满足格式要求,且 x 和 y 在 (0,1000) 之间。
- 计算距离:计算每个合法坐标到总部 (0,0) 的距离。
- 找出最远的坐标:在所有合法坐标中找出距离最大的那个。
- 处理异常情况:如果没有合法坐标,返回总部坐标 (0,0)。
- function findFarthestCoordinate(data) {
- // 定义正则表达式以提取合法的坐标
- const pattern = /\((\d{1,3}),(\d{1,3})\)/g;
- let matches;
- let maxDistance = 0;
- let farthestCoordinate = "(0,0)";
-
- while ((matches = pattern.exec(data)) !== null) {
- const xStr = matches[1];
- const yStr = matches[2];
- const x = parseInt(xStr, 10);
- const y = parseInt(yStr, 10);
-
- // 检查坐标是否合法
- if (isValidCoordinate(xStr, yStr, x, y)) {
- const distance = x * x + y * y;
- if (distance > maxDistance) {
- maxDistance = distance;
- farthestCoordinate = `(${x},${y})`;
- }
- }
- }
-
- return farthestCoordinate;
- }
-
- // 验证坐标是否合法
- function isValidCoordinate(xStr, yStr, x, y) {
- return x > 0 && x < 1000 && y > 0 && y < 1000 &&
- !(xStr.startsWith("0") && xStr.length > 1) &&
- !(yStr.startsWith("0") && yStr.length > 1);
- }
-
- // 示例输入
- const inputData = "ferga13fdsf3(100,200)f2r3rfasf(300,400)";
- console.log(findFarthestCoordinate(inputData)); // 输出: (300,400)
在JavaScript中,我们使用正则表达式提取字符串中的坐标。
- const pattern = /\((\d{1,3}),(\d{1,3})\)/g;
- let matches;
解析思路:
\((\d{1,3}),(\d{1,3})\) 匹配形如 (x,y) 的坐标,其中 x 和 y 是 1 到 3 位的数字。pattern.exec(data) 用于在输入字符串中查找所有符合正则表达式的子字符串。通过解析得到的字符串,进一步验证它们是否合法。
- function isValidCoordinate(xStr, yStr, x, y) {
- return x > 0 && x < 1000 && y > 0 && y < 1000 &&
- !(xStr.startsWith("0") && xStr.length > 1) &&
- !(yStr.startsWith("0") && yStr.length > 1);
- }
验证思路:
计算每个合法坐标到总部 (0,0) 的距离。
const distance = x * x + y * y;
计算思路:
distance = x * x + y * y。在所有合法坐标中找出距离最大的那个。
- if (distance > maxDistance) {
- maxDistance = distance;
- farthestCoordinate = `(${x},${y})`;
- }
思路:
maxDistance 记录最大距离,每次计算新的距离后与之比较,更新最大距离和最远坐标。如果没有合法坐标,最远的坐标默认为 (0,0)。
- let maxDistance = 0;
- let farthestCoordinate = "(0,0)";
思路:
(0,0),如果找到合法坐标,更新为最远坐标。通过上述步骤,我们可以在JavaScript中实现解析字符串并找出距离总部最远的合法坐标,并处理非法坐标和无坐标的情况。
在上述问题中,我们通过解析记录字符串找到距离总部 (0,0) 最远的合法坐标。我们分别使用了Java、C、Python和JavaScript四种语言实现了解决方案。
下期见啦~🥰