首页 > 试题广场 >

坐标移动

[编程题]坐标移动
  • 热度指数:563949 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。

输入:

合法坐标为A(或者D或者W或者S) + 数字(两位以内)

坐标之间以;分隔。

非法坐标点需要进行丢弃。如AA10;  A1A;  $%$;  YAD; 等。

下面是一个简单的例子 如:

A10;S20;W10;D30;X;A1A;B10A11;;A10;

处理过程:

起点(0,0)

+   A10   =  (-10,0)

+   S20   =  (-10,-20)

+   W10  =  (-10,-10)

+   D30  =  (20,-10)

+   x    =  无效

+   A1A   =  无效

+   B10A11   =  无效

+  一个空 不影响

+   A10  =  (10,-10)

结果 (10, -10)

数据范围:每组输入的字符串长度满足 ,坐标保证满足 ,且数字部分仅含正数

输入描述:

一行字符串



输出描述:

最终坐标,以逗号分隔

示例1

输入

A10;S20;W10;D30;X;A1A;B10A11;;A10;

输出

10,-10
示例2

输入

ABC;AKL;DA1;

输出

0,0
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        String[] strings = input.split(";");
        List<String> ops = new ArrayList<>();
        for (String str : strings) {
            if (str.matches("[WASD]\\d{1,2}")) {
                ops.add(str);
            }
        }
        int x = 0;
        int y = 0;
        for (String op : ops) {
            if (op.startsWith("A")) {
                x = x - Integer.parseInt(op.substring(1));
            }
            if (op.startsWith("D")) {
                x = x + Integer.parseInt(op.substring(1));
            }
            if (op.startsWith("W")) {
                y = y + Integer.parseInt(op.substring(1));
            }
            if (op.startsWith("S")) {
                y = y - Integer.parseInt(op.substring(1));
            }
        }
        System.out.println(x + "," + y);
    }
}
编辑于 2024-04-16 12:33:05 回复(0)
1. 合法性 A/W/S/D + 一个数字0 ~ 99 ,排行里面两位数字有很多人没有校验,竟然通过了。
2. 搞不懂这个执行时间相差这么多,有的人的代码跟我一样的逻辑,我的还更好一些,竟然没有他们运行地快,奇怪。
编辑于 2024-03-23 21:26:41 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int x = 0;
            int y = 0;
            String input = sc.next();
            String[] arr = input.split(";");
            for (String s : arr) {
                if (s.equals("") || s.length() < 2) {
                    continue;
                }
                String reg = "^[ADWS]{1}[0-9]{1,2}$";
                if (!s.matches(reg)) {
                    continue;
                }
                char direct = s.charAt(0);
                int dt = Integer.parseInt(s.substring(1));
                switch (direct) {
                    case 'A':
                        x = x - dt;
                        break;
                    case 'D':
                        x = x + dt;
                        break;
                    case 'W':
                        y = y + dt;
                        break;
                    case 'S':
                        y = y - dt;
                        break;
                    default:
                        continue;
                }
            }
            System.out.println(x + "," + y);
        }
    }
}

编辑于 2024-03-16 18:31:55 回复(0)
import java.util.*;

class Main {
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int r1 = 0, r2 = 0;
        String[] arr = str.split(";");
        for (String t : arr) {
            if (t.length() < 2 || t.length() > 3) {
                continue;
            }
            int s = 0;
            if (t.charAt(1) >= '0' && t.charAt(1) <= '9')  {
                s = t.charAt(1) - '0';
                if (t.length() > 2 && t.charAt(2) >= '0' && t.charAt(2) <= '9')  {
                    s *= 10;
                    s += t.charAt(2) - '0';
                } else if(t.length() > 2){
                    continue;
                }
            } else {
                continue;
            }
            switch (t.charAt(0)) {
                case 'A': r1 -= s;break;
                case 'D': r1 += s;break;
                case 'W': r2 += s;break;
                case 'S': r2 -= s;break; 
            }
        }
        System.out.println(r1 + "," + r2);
    }

}

编辑于 2024-03-15 21:14:30 回复(0)
想不起正则怎么写的我,认真说,同样是中等难度,这道题和上一个背包算法的那个完全不是一个量级
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int x = 0;
        int y = 0;
        String par = in.nextLine();
        if (par != null && par.length() > 0) {
            String[] values = par.split(";");
            for (String v : values) {
                if (v != null && v.length() > 1) {
                    String a = v.substring(0, 1);
                    String b = v.substring(1, v.length());
                    try {
                        int c = Integer.parseInt(b);
                        switch (a) {
                            case "A" :
                                x = x - c;
                                break;
                            case "D" :
                                x = x + c;
                                break;
                            case "W" :
                                y = y + c;
                                break;
                            case "S" :
                                y = y - c;
                                break;
                            default:
                                break;
                        }
                    } catch (Exception e) {
                    }
                }
            }
        }
        System.out.print(x + "," + y);

    }
}


编辑于 2024-03-15 10:52:19 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String inputStr =  in.nextLine();
        String[] strArr = inputStr.split(";");
        int x = 0;
        int y = 0;
        int v = 0;
        char direct;
        for (int i = 0; i < strArr.length; i++) {
            if (strArr[i].length() > 1) {
                direct = strArr[i].charAt(0);
                try {
                    v = Integer.parseInt(strArr[i].substring(1, strArr[i].length()).trim());
                    switch (direct) {
                        case 'A':
                            x -= v;
                            break;
                        case 'D':
                            x += v;
                            break;
                        case 'W':
                            y += v;
                            break;
                        case 'S':
                            y -= v;
                            break;
                        default:
                    }
                } catch (Exception e) {
                    continue;
                }
            }
        }
        System.out.println(x + "," + y);
    }
}

编辑于 2024-01-03 16:48:58 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {

    public static void main(String[] args) {
       
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String s = in.nextLine();
            String[] arr = s.split(";");
            String regx = "[AWDS][0-9][0-9]?";
            int x = 0, y = 0;
            for(int i = 0; i < arr.length; i++) {
                if (arr[i].matches(regx)) {
                    Integer subValue = Integer.valueOf(arr[i].substring(1,arr[i].length()));
                    switch(arr[i].substring(0,1)) {
                        case "A":
                            x -= subValue;
                            break;
                        case "D":
                            x += subValue;
                            break;
                        case "W":
                            y += subValue;
                            break;
                        case "S":
                            y -= subValue;
                            break;
                        default:
                            break;            
                    }
                }
            }
            System.out.println(x +","+ y);
        }
    }
}

解题思路:
    首先将输入的字符串按照“;”分割成字符串数组,然后对字符串数组中元素值进行判断;1、首先判断字符串数组元素首字母是否为[AWSD]其中之一,然后判断字符串数组元素首字母后元素是否为数字。这里我们判断时使用正则表达式判断比较方便,编写正则表达式也是最核心的,如果正则表达式不正确,则过滤的结果肯定不正确。
    String regx = "[AWSD][0-9][0-9]?";
     第一个[AWSD],表示第一个字母为“AWSD”其中之一,第二个[0-9]表示字符串数组元素第二个值为“0-9”其中之一;第三个[0-9]?表示“0-9”其中之一出现0次或者一次。因为字符串数组值可能是“A3”、“D7”之类的。
     判断处理完字符串数组元素之后,我们要对符合条件的字符串数组元素值进行处理。
      定义横坐标x,纵坐标y;
     A:代表坐标轴中横坐标x左移,移动单位为字符串数组元素值数字部分;
     D:代表坐标轴中横坐标x右移,移动单位为字符串数组元素值数字部分;
     W: 代表坐标轴中纵坐标y上移,移动单位为字符串数组元素值数字部分;
     S: 代表坐标轴中纵坐标y下移,移动单位为字符串数组元素值数字部分;
最后根据字符串数组元素值匹配,然后对x和y进行相应的加减操作,最后将结果输出打印;
发表于 2023-12-03 22:32:22 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str =in.nextLine();
            String[] str1=str.split(";");
    //创建一个长度为2的整数数组用来装横坐标和纵坐标;横坐标为ic[0],纵坐标为ic[1]
            int[] ic=new int[2];
            //循环遍历输入的字符数组
            for(int i=0;i<str1.length;i++){
                if(str1[i].length()==3){
                    //创建一个临时变量用来做横纵坐标加减
                    String tmp=str1[i].substring(1,3);
                    //判断数组首位是否为ASDW
                    switch(str1[i].substring(0,1)){
        //判断数组首位为A的,第二三位是否为数字,如果为整数,则横坐标减去该数字
                        case "A":
                        if(isInteger(tmp)==true){
                           ic[0]-=Integer.parseInt(tmp);
                        }
                        break;
        //判断数组首位为D的,第二三位是否为数字,如果为整数,则横坐标增加该数字
                        case "D":
                        if(isInteger(tmp)==true){
                            ic[0]+=Integer.parseInt(tmp);
                        }
                        break;
        //判断数组首位为S的,第二三位是否为数字,如果为整数,则纵坐标减去该数字
                        case "S":
                        if(isInteger(tmp)==true){
                            ic[1]-=Integer.parseInt(tmp);
                        }
                        break;
        //判断数组首位为W的,第二三位是否为数字,如果为整数,则纵坐标增加该数字
                        case "W":
                        if(isInteger(tmp)==true){
                            ic[1]+=Integer.parseInt(tmp);
                        }
                        break;
                        }
                       
                }
            }
            System.out.println(ic[0]+","+ic[1]);
        }
    }
    /**
     * 判断字符串是否是整数
     */
    private static boolean isInteger(String value) {
        try {
            Integer.parseInt(value);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

}
发表于 2023-12-01 14:29:35 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        String []StrArr = input.split(";");
        int x = 0;
        int y = 0;
        for (String str : StrArr) {
            if (str.length() != 0 && str.length() != 1  &&
                    str.substring(1, str.length()).matches("\\d+")) {
                int tem = Integer.parseInt(str.substring(1, str.length()));
                switch (str.substring(0, 1)) {
                    case "A":
                        x = x - tem;
                        break;
                    case "S":
                        y = y - tem;
                        break;
                    case "D":
                        x = x + tem;
                        break;
                    case "W":
                        y = y + tem;
                        break;
                    default:
                        break;
                }
            }
        }
        System.out.print(x + "," + y);
    }
}

发表于 2023-10-28 17:37:27 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int x = 0; // 初始 x 坐标
int y = 0; // 初始 y 坐标
String str = in.nextLine();
String[] part = str.split(";");
String regex = "([ADWS]\\d{1,2})";
for (int i = 0; i < part.length; i++) {
if (part[i].matches(regex)) {
char dir = part[i].charAt(0);
// System.out.println(dir);
int dis = Integer.parseInt(part[i].substring(1, part[i].length()));
// System.out.println(dis);
// System.out.println(part[i]);
switch (dir) {
case'A':
x -= dis;
break;
case'W':
y += dis;
break;
case'S':
y -= dis;
break;
case'D':
x += dis;
break;
}
}
}
System.out.print(x + "," + y);
}
}

发表于 2023-10-16 15:31:27 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    private static int x;
    private static int y;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String regex = "-?[1-9]\\d*";
        while (in.hasNext()) { // 注意 while 处理多个 case
            x = 0;
            y = 0;
            String str = in.next();
            String[] split = str.split(";");
            for(String s : split) {
                if(s.trim().length() > 1){
                    char ch = s.charAt(0);
                    String num = s.substring(1);
                    if(num.matches(regex)){
                        move(Integer.parseInt(num),ch);
                    }
                }
            }
            System.out.println(x+","+y);
        }
    }

    public static void move(int num, char ch) {
        if(ch == 'A'){
            x -= num;
        }else if(ch == 'D') {
            x += num;
        }else if (ch == 'W') {
            y += num;
        }else if (ch == 'S') {
            y -= num;
        }
    }
}
发表于 2023-10-16 11:23:41 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        String str=in.nextLine();
        int len=0;
        int x=0;
        int y=0;
        for(int i=0;i<str.length();i++){
            if(str.charAt(i)==';'){
                len++;
            }
            if(i==str.length()-1&&str.charAt(i)!=';'){
                len++;
            }
        }
        String arr[]=new String[len];
        int l=-1;
        for(int i=-1;i<str.length();i++){
            if(i==-1||str.charAt(i)==';'){
                if(i==str.length()-1){
                    break;
                }else{
                    arr[l+1]="a";
                    l++;
                }
            }else{
                arr[l]=arr[l]+str.charAt(i);
                if(i==str.length()-1){
                    break;
                }
            }
        }
        for(int i=0;i<len;i++){
            if(arr[i].length()==3){
                try{
                    if(arr[i].charAt(1)=='A'){
                        x=x-Integer.valueOf(String.valueOf(arr[i].charAt(2)));
                    }else if(arr[i].charAt(1)=='D'){
                        x=x+Integer.valueOf(String.valueOf(arr[i].charAt(2)));
                    }else if(arr[i].charAt(1)=='W'){
                        y=y+Integer.valueOf(String.valueOf(arr[i].charAt(2)));
                    }else if(arr[i].charAt(1)=='S'){
                        y=y-Integer.valueOf(String.valueOf(arr[i].charAt(2)));
                    }
                }catch(Exception e){
                    i++;
                }
            }else if(arr[i].length()==4) {
                try {
                    if (arr[i].charAt(1) == 'A') {
                        x = x - Integer.valueOf(String.valueOf(arr[i].charAt(2))) * 10 - Integer.valueOf(String.valueOf(arr[i].charAt(3)));
                    } else if (arr[i].charAt(1) == 'D') {
                        x = x + Integer.valueOf(String.valueOf(arr[i].charAt(2))) * 10 + Integer.valueOf(String.valueOf(arr[i].charAt(3)));
                    } else if (arr[i].charAt(1) == 'W') {
                        y = y + Integer.valueOf(String.valueOf(arr[i].charAt(2))) * 10 + Integer.valueOf(String.valueOf(arr[i].charAt(3)));
                    } else if (arr[i].charAt(1) == 'S') {
                        y = y - Integer.valueOf(String.valueOf(arr[i].charAt(2))) * 10 - Integer.valueOf(String.valueOf(arr[i].charAt(3)));
                    }
                } catch (Exception e) {
                    i++;
                }
            }
        }
        System.out.println(x+","+y);
    }
}
发表于 2023-09-14 01:14:07 回复(1)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] coordinate= {0, 0};
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String[] str=in.nextLine(). split(";");
            for (int i = 0; i < str.length; i++) {
                coordinate=move(coordinate,str[i]);
            }
            System.out.println(coordinate[0]+","+coordinate[1]);
        }
    }
    public static int[] move(int[] cdn,String str)
    {
        int[] coordinate=cdn;
        int dis=0;
        String[] split=str.split("");
        if (checkLegal(str)!=true){
            return coordinate;
        }
        if (str.length()==2){
            dis=Integer.parseInt(split[1]);
        }
        else if(str.length()==3)
        {
            dis=Integer.parseInt(split[1]+split[2]);
        }
        switch (String.valueOf(split[0])){
            case "W" :
            cdn[1]+=dis;
                break;
            case "A" :
                cdn[0]-=dis;
                break;
            case "S" :
                cdn[1]-=dis;
                break;
            case "D" :
                cdn[0]+=dis;
                break;
        }

        return coordinate;
    }
    public static boolean checkLegal(String str){
        if (str.length()>=4)
            return false;
        for (int i = 1; i < str.length(); i++) {
            if(str.charAt(i)<'0'||str.charAt(i)>'9')
            {return false;}
        }
        return true;
    }
}

发表于 2023-09-04 22:56:25 回复(0)
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {

        int[] start = new int[] {0, 0};

        HashMap<Character, Integer[]> actions = new HashMap<>();
        actions.put('D', new Integer[] {1, 0});
        actions.put('A', new Integer[] {-1, 0});
        actions.put('W', new Integer[] {0, 1});
        actions.put('S', new Integer[] {0, -1});
        Scanner in = new Scanner(System.in);
        String positions = in.nextLine();
        String[] positionArray = positions.split(";");
        for (String s : positionArray) {
            try {
                Integer[] integers = actions.get(s.charAt(0));
                int parseInt = Integer.parseInt(s.substring(1));
                start[0] += integers[0] * parseInt;
                start[1] += integers[1] * parseInt;
            } catch (Exception ignored) {
            }
        }
        System.out.println(Arrays.toString(start).replace("[", "").replace("]",
                           "").replace(" ", ""));
    }
}

发表于 2023-08-09 02:27:06 回复(0)
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();

        String[] split = line.split(";");
        int x = 0;
        int y = 0;
        for (int i = 0; i < split.length; i++) {
            String word = split[i];
            if (isLegal(word)) {
                char c = word.charAt(0);
                int number = Integer.parseInt(word.substring(1));
                if (c == 'A') {
                    x -= number;
                }

                if (c == 'D') {
                    x += number;
                }

                if (c == 'S') {
                    y -= number;
                }

                if (c == 'W') {
                    y += number;
                }
            }
        }

        //StringBuilder sb = new StringBuilder();
        //sb.append( x + "," + y );
        System.out.println(x + "," + y);
    }

    /**
     * 判断是否合法字母+数字(两位以内)
     *
     * @param word
     * @return
     */
    public static boolean isLegal(String word) {
        if (word.length() >= 2 && word.length() <= 3) {
            if ("WADS".contains(word.charAt(0) + "")) {
                int number = 0;
                try {
                    number = Integer.parseInt(word.substring(1));
                } catch (NumberFormatException e) {
                    return false;
                }
                if (number > 0 && number <= 99) {
                    return true;
                }
            }
        }

        return false;
    }
}

发表于 2023-08-05 11:14:53 回复(0)
不会正则用了个最笨的办法
import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        // while (in.hasNextInt()) { // 注意 while 处理多个 case
        //     int a = in.nextInt();
        //     int b = in.nextInt();
        //     System.out.println(a + b);
        // }
        String strings = in.nextLine();
        String[] split = strings.split(";");
        int x = 0;
        int y = 0;
        for (String str : split) {
            if (str.length() == 3 || str.length() == 2) {
                if (isChar(str.charAt(0)) && isNumber(str.substring(1))) {
                    char c = str.charAt(0);
                    int value = Integer.parseInt(str.substring(1));
                    switch (c) {
                    case 'A':
                        x -= value;
                        break;
                    case 'W':
                        y += value;
                        break;
                    case 'S':
                        y -= value;
                        break;
                    case 'D':
                        x += value;
                }
                }
            }
        }
        System.out.println(x + "," + y);
    }

    public static boolean isChar(char c) {
        return c == 'A' || c == 'S' || c == 'W' || c == 'D';
    }

    public static boolean isNumber(String str) {
        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}

发表于 2023-07-21 22:45:44 回复(0)
不会正则表达式,用了些奇怪的方法
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    static HashSet hashSet = new HashSet();

    public static void main(String[] args) {

        hashSet.add('A');
        hashSet.add('W');
        hashSet.add('S');
        hashSet.add('D');
        Scanner scanner = new Scanner(System.in);
        String strings = scanner.nextLine();
        String[] split = strings.split(";");
        int x = 0, y = 0;
        for (String s : split) {
            Control control = testRight(s);
            if (control.isFlag() == true) {
                char awsd = control.getAWSD();
                Integer value = control.getValue();
                switch (awsd) {
                    case 'A':
                        x -= value;
                        break;
                    case 'W':
                        y += value;
                        break;
                    case 'S':
                        y -= value;
                        break;
                    case 'D':
                        x += value;
                }
            }

        }
        System.out.println(x + "," + y);

    }

    public  static Control testRight(String s) {
        Control control = new Control();
        if (s.length() > 3 || s.length() < 2)
            return control;
        char one = s.charAt(0);
        if (!hashSet.contains(one))
            return control;
        String ints = "";
        for (int i = 1; i < s.length(); i++) {
            ints += s.charAt(i) + "";
        }
        try {
            Integer value = Integer.valueOf(ints);
            control.setValue(value);
        } catch (NumberFormatException e) {
            return control;
        }
        control.setAWSD(one);
        control.setFlag(true);
        return control;

    }

    static class Control {
        private boolean flag = false;
        private char AWSD;
        private Integer value;

        public boolean isFlag() {
            return flag;
        }


        public void setFlag(boolean flag) {
            this.flag = flag;
        }

        public char getAWSD() {
            return AWSD;
        }

        public void setAWSD(char AWSD) {
            this.AWSD = AWSD;
        }

        public Integer getValue() {
            return value;
        }

        public void setValue(Integer value) {
            this.value = value;
        }
    }
}


发表于 2023-07-01 22:16:50 回复(0)

实在是没找到哪里有问题

查了下正则语法,好像也没有问题,但就是匹配不到个别字符串

import java.util.*;
import java.util.regex.*;

public class Main {
    public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
        int[] result = new int[2];
        while (in.hasNextLine()) {
            String str = in.nextLine();
            //正则
            String regex = "^([WSAD]\\d{1,2});|;([WSAD]\\d{1,2});";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()) { //匹配第一位
                String s = matcher.group(1);
                if (s != null) {
                    complete(result, s);
                }
            }
            while (matcher.find()) { //匹配非第一位
                String s = matcher.group(2);
                if (s != null) {
                    complete(result, s);
                }
            }
            System.out.printf("%d,%d\n", result[0], result[1]);
        }
    }
    //解析、计算指令
    public static void complete(int[] result, String s) {
       int num = Integer.parseInt(s.substring(1, s.length()));
        if (s.charAt(0) == 'W') {
            result[1] += num;
        } else if (s.charAt(0) == 'S') {
            result[1] -= num;
        } else if (s.charAt(0) == 'A') {
            result[0] += num;
        } else if (s.charAt(0) == 'D') {
            result[0] -= num;
        }
    }
}

第一个测试用例都没过

A10;S20;W10;D30;X;A1A;B10A11;;A10;

正则在匹配的时候匹配不到S20和D30
有没有大佬知道为啥呀

发表于 2023-06-13 13:39:35 回复(0)

问题信息

难度:
249条回答 92824浏览

热门推荐

通过挑战的用户

查看代码