赞
踩
题目描述:
某探险队负责对地下洞穴进行探险。探险队成员在进行探险任务时,随身携带的记录器会不定期地记录自身的坐标,但在记录的间隙中也会记录其他数据。探索工作结束后,探险队需要获取到某成员在探险过程中相对于探险队总部的最远的足迹位置。
1. 仪器记录坐标时,坐标的数据格式为(x,y),如(1,2)、(100,200),其中0<x<1000,0<y<1000。同时存在非法坐标,如(01,1)、(1,01),(0,100)属于非法坐标。
2. 设定探险队总部的坐标为(0,0),某位置相对总部的距离为:x*x+y*y。
3. 若两个座标的相对总部的距离相同,则第一次到达的坐标为最远的足迹。
4. 若记录仪中的坐标都不合法,输出总部坐标(0,0)。
备注:不需要考虑双层括号嵌套的情况,比如sfsdfsd((1,2))。
输入描述:
字符串,表示记录仪中的数据。
如:ferga13fdsf3(100,200)f2r3rfasf(300,400)
输出描述:
字符串,表示最远足迹到达的坐标。
如: (300,400)补充说明:
收起
示例1
输入:
ferg(3,10)a13fdsf3(3,4)f2r3rfasf(5,10)输出:
(5,10)说明:
记录仪中的合法坐标有3个: (3,10), (3,4), (5,10),其中(5,10)是相距总部最远的坐标, 输出(5,10)。
示例2
输入:
asfefaweawfaw(0,1)fe输出:
(0,0)说明:
记录仪中的坐标都不合法,输出总部坐标(0,0)
解题思路:
使用正则表达式,对字符串处理
- var readline = require('readline');
- const rl = readline.createInterface({
- input: process.stdin,
- output: process.stdout
- });
- rl.on('line', function(line: string){
- const posList: number[][] = [];
- let i = 0;
- while(line.indexOf("(", i) !== -1) {
- i = line.indexOf("(", i) + 1;
- const disStr = line.slice(i, line.indexOf(")", i));
- const [aStr, bStr] = disStr.split(",");
- if(aStr.startsWith("0") || bStr.startsWith("0")) {
- continue;
- }
- const a = Number(aStr), b = Number(bStr);
- if(a > 0 && a < 1000 && b > 0 && b < 1000) {
- posList.push([a, b]);
- }
- }
- let maxDis = -Infinity, maxPos = [0, 0];
- posList.forEach(([a, b]) => {
- const dis = a * a + b * b;
- if(dis > maxDis) {
- maxDis = dis;
- maxPos = [a, b];
- }
- })
- console.log(`(${maxPos[0]},${maxPos[1]})`)
- });
- import sys
- s = input()
-
- stack = []
- res = []
- i = 0
- while i < len(s):
- if s[i] == '(' and len(stack) == 0:
- stack.append(s[i])
- j = i+1
- while s[j] != ')' and j < len(s):
- j += 1
- res.append(s[i:j+1])
- stack.pop()
- i = j
- else:
- i+=1
-
- ans = (0, 0)
-
- max_sum = 0
- for i in res:
- sum1 = 0
- tmp = i.strip('()')
- l = tmp.split(',')
- if len(l) != 2:
- print(ans)
- sys.exit()
- for j in l:
- if not j.isdigit():
- print(ans)
- sys.exit()
- elif j[0] == '0':
- print(ans)
- sys.exit()
- elif int(j) <= 0 or int(j) >= 1000:
- print(ans)
- sys.exit()
- else:
- sum1 += int(j)*int(j)
- max_sum = max(max_sum, sum1)
- ans = i
-
- print(ans)
- import java.util.*;
- import java.util.regex.Pattern;
-
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String str = sc.nextLine();
- String[] arr1 = str.split("\\(");
- String[] arr2 = new String[arr1.length];
- int max = 0, maxX = 0, maxY = 0;
- int j = 0;
- for(int i = 0; i < arr1.length; i++){
- if(arr1[i].contains(")")){
- arr2[j] = arr1[i].split("\\)")[0];
- j++;
- }
- }
- Pattern pattern = Pattern.compile("[1-9]+[0-9]*,[1-9]+[0-9]*");
- for(int i = 0; i < j; i++){
- if(pattern.matcher(arr2[i]).matches()){
- int a = Integer.valueOf(arr2[i].split(",")[0]);
- int b = Integer.valueOf(arr2[i].split(",")[1]);
- if(a<1000&&b<1000&&a*a+b*b>max){
- max = a*a+b*b;
- maxX = a;
- maxY = b;
- }
- }
- }
- System.out.println("("+maxX+","+maxY+")");
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。