在一行上输入一个长度
,仅由可见字符构成的字符串
,代表输入的指令序列。
在一行上输出一个两个整数,代表小人最终位置的横纵坐标,使用逗号间隔。
A10;S20;W10;D30;X;A1A;B10A11;;A10;
10,-10
对于这个样例,我们模拟小人的移动过程:
第一个指令
是合法的,向左移动
个单位,到达
点;
第二个指令
是合法的,向下移动
个单位,到达
点;
第三个指令
是合法的,向上移动
个单位,到达
点;
第四个指令
是合法的,向右移动
个单位,到达
点;
第五个指令
不合法,跳过;
第六个指令
不合法,跳过;
第七个指令
不合法,跳过;
第八个指令
不合法,跳过;
第九个指令
是合法的,向左移动
个单位,到达
点。
ABC;AKL;DA1;
0,0
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); String[] arr = s.split(";"); int x = 0; int y = 0; for(String a : arr) { if(a == null || a.trim().length() < 2 || a == "") { continue; } char cur = a.charAt(0); if(cur=='A'||cur=='D'||cur=='W'||cur=='S') { String ss = a.substring(1); boolean judge = true; //判断后面的步数指令有没有字母 for(int i = 0;i < ss.length();i++) { if(ss.charAt(i) > '9' || ss.charAt(i) < '0') { judge = false; break; } } if(judge == false) { continue; } else { int step = Integer.parseInt(ss); if(step < 1 || step > 100) { continue; } if(cur == 'A') { x -= step; } else if(cur == 'D') { x += step; } else if(cur == 'W') { y += step; } else if(cur == 'S') { y -= step; } } } else { continue; } } System.out.print(x + "," + y); } }
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 int[][] indexNum = new int[1][2];//默认0 0 String arr = in.nextLine(); arr=arr.replaceAll(";;",";"); String[] str=arr.split(";"); for(String s:str){ if(s.length()>3||s.length()<2) continue; if(!s.matches(".*[^ASDW]+")) continue; if((!s.matches("^[^ASDW]*[ASDW][^ASDW]*$"))) continue; if(s.charAt(0)=='A'){ indexNum[0][0]= indexNum[0][0]-Integer.valueOf(s.substring(1,s.length())); }else if(s.charAt(0)=='S'){ indexNum[0][1]= indexNum[0][1]-Integer.valueOf(s.substring(1,s.length())); }else if(s.charAt(0)=='D'){ indexNum[0][0]= indexNum[0][0]+Integer.valueOf(s.substring(1,s.length())); }else if(s.charAt(0)=='W'){ indexNum[0][1]= indexNum[0][1]+Integer.valueOf(s.substring(1,s.length())); } } System.out.println(indexNum[0][0]+","+indexNum[0][1]); } } }
public static void printCoordinate(){ Scanner in = new Scanner(System.in); System.out.println("enter coordinates: "); // A10;S20;W10;D30;X;A1A;B10A11;;A10; String input = in.nextLine(); String[] strIn = input.split(";"); System.out.println(Arrays.toString(strIn)); int x = 0; int y = 0; for (String step : strIn){ if (step.charAt(0) == 'W' || step.charAt(0) == 'S' || step.charAt(0) == 'A' || step.charAt(0) == 'D'){ String stepStr = step.substring(1, 3); if (step.length() >= 3 || step.trim().isEmpty()) { continue; } else{ try{ int stepNum = Integer.parseInt(stepStr); if (0 < stepNum && stepNum < 100){ if (step.charAt(0) == 'W'){ y += stepNum; }else if(step.charAt(0) == 'S'){ y -= stepNum; }else if(step.charAt(0) == 'A'){ x -= stepNum; }else if(step.charAt(0) == 'D'){ x += stepNum; } } }catch(NumberFormatException e){ continue; } } } } System.out.println(x + "," + y); }有没有大佬可以指点一下为什么我这个处理不了只有一个;的情况啊
import java.util.LinkedList; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); String[] split = s.split(";"); int x = 0; int y = 0; for (int i = 0; i < split.length; i++) { int move = islegal(split[i]); if (move != -1) { //指令合法 char direction = split[i].charAt(0); if (direction == 'A') { x -= move; } else if (direction == 'D') { x += move; } else if (direction == 'W') { y += move; } else if (direction == 'S') { y -= move; } } } System.out.println(x + "," + y); } public static int islegal(String s) { //返回距离 if (s.length() < 2) return -1; char direction = s.charAt(0); if (direction == 'A' || direction == 'W' || direction == 'D' || direction == 'S') { String move = s.substring(1, s.length()); if (move.isEmpty()) return -1; int m = 0; try { m = Integer.valueOf(move); } catch (Exception e) { //不能将字符串转为数字 return -1; } if (m >= 1 && m < 100) return m; } return -1; } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int x = 0, y = 0; String s = in.next(); String[] sp = s.split(";"); for (int i = 0; i < sp.length; i++) { if (sp[i].length() < 2 || sp[i].length() > 3) // 初步筛选长度不合规的 continue; String move = sp[i].substring(0, 1); if (move.matches("[ADWS]")) { s = sp[i].substring(1); if (s.matches("[0-9]{1,2}")) { // 1-99 int d = Integer.parseInt(s); if (move.equals("A")) { x -= d; } else if (move.equals("D")) { x += d; } else if (move.equals("W")) { y += d; } else if (move.equals("S")) { y -= d; } } } } System.out.print(x + "," + y); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { private static int X = 0; private static int Y = 0; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String input; do { input = scanner.nextLine(); String[] strArrays = input.split(";"); for (String temp : strArrays) { move(temp); } System.out.println(X + "," + Y); } while (scanner.hasNext() && !"0".equals(input)); } public static void move(String input) { if (input.isEmpty() || input.length() > 3) { return; } String direction = input.substring(0, 1); String value = input.substring(1); try { int intValue = Integer.parseInt(value); if (intValue < 1 || intValue > 99) { return; } } catch (Exception e) { return; } if ("A".equals(direction)) { X -= Integer.parseInt(value); } if ("D".equals(direction)) { X += Integer.parseInt(value); } if ("W".equals(direction)) { Y += Integer.parseInt(value); } if ("S".equals(direction)) { Y -= Integer.parseInt(value); } } }
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(); // 分割字符串得到命令数组 String[] commands = str.split(";"); int x = 0, y = 0; for(int i = 0; i < commands.length; i++) { if(isValid(commands[i])) { char dir = commands[i].charAt(0); int len = Integer.parseInt(commands[i].substring(1)); if(dir == 'A') { x -= len; } else if(dir == 'D') { x += len; } else if(dir == 'W') { y += len; } else if(dir == 'S') { y -= len; } } } System.out.print(x + "," + y); } // 检查命令是否有效 public static boolean isValid(String str) { if(str.equals(" ") || str.length() > 3 || str.length() < 2) { return false; } // 如果命令长度是3,检查要第三个字符 if(str.length() == 3 && !isDigit(str.charAt(2))) { return false; } if(isLetter(str.charAt(0)) && isDigit(str.charAt(1))) { return true; } return false; } // 检查命令的方向操作符是否有效 public static boolean isLetter(char c) { if(c == 'A' || c == 'D' || c == 'W' || c == 'S') { return true; } return false; } // 检查字符是否是数字 public static boolean isDigit(char c) { if(c >= '0' && c <= '9') { return true; } return false; } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { static class Coordinate{ int x; int y; public Coordinate(){ this.x=0; this.y=0; } public void move(char direction,int dist){ switch(direction){ case 'W': this.y+=dist; break; case 'S': this.y-=dist; break; case 'A': this.x-=dist; break; case 'D': this.x+=dist; break; default: return; } } public String GetCoordinate(){ StringJoiner sj=new StringJoiner(","); return sj.add(this.x+"").add(this.y+"").toString(); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s=sc.nextLine(); System.out.println(GetResult(s)); } //判断一个字符串是否为整数 public static boolean isInteger(String str) { try { Integer.parseInt(str); return true; } catch (NumberFormatException e) { return false; } } public static String GetResult(String s){ String[] cds=s.split(";"); Coordinate Cd=new Coordinate(); for(int i=0;i<cds.length;i++){ //去掉第一个字符,看剩下的是否为一个整数 if(!isInteger(cds[i].substring(1))){ i++; continue; } else{ char direction=cds[i].charAt(0); int dist=Integer.parseInt(cds[i].substring(1)); Cd.move(direction,dist); } } return Cd.GetCoordinate(); } }
判断是否为空,判断开头是否为合法操作,判断结尾是否为数字,没有判断是否存在AS7这种情况 import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Map<Character, Integer> map = new HashMap<>(); map.put('A', -1); map.put('D', 1); map.put('S', -1); map.put('W', 1); Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String source = in.next(); String[] arr = source.split(";"); int x = 0; int y = 0; for(int i = 0; i < arr.length; i ++) { // 判断坐标合法性 String temp = arr[i].trim(); if(temp.length() ==3||temp.length()==2) {//过滤空的 char c = temp.charAt(0); if(temp.charAt(temp.length()-1)==' '||!(temp.charAt(temp.length()-1)<='9'&&temp.charAt(temp.length()-1)>='0')) { continue; } if(map.containsKey(c)) {//操作符合ADWS //读取后面两位的内容 //为啥不用判断是否是AKL,DA1这种的 int num=0; if(temp.length() ==3) { int s=Character.getNumericValue(temp.charAt(1)); int g=Character.getNumericValue(temp.charAt(2)); num=s*10+g; } else{ int s=Character.getNumericValue(temp.charAt(1)); num=s; } try { int numValue = Integer.valueOf(num);// if('A' == c || 'D' == c) { x = x + map.get(c) * numValue; }else { y = y + map.get(c) * numValue; } } catch(Exception e) { continue; } } } } System.out.println(x+","+y); } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Map<Character, Integer> map = new HashMap<>(); map.put('A', -1); map.put('D', 1); map.put('S', -1); map.put('W', 1); Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String source = in.next(); String[] arr = source.split(";"); int x = 0; int y = 0; for(int i = 0; i < arr.length; i ++) { // 判断坐标合法性 String temp = arr[i]; if(temp.length() > 0) { char c = temp.charAt(0); if(map.containsKey(c)) { //读取后面两位的内容 String num = temp.substring(1, temp.length()); try { int numValue = Integer.valueOf(num); if('A' == c || 'D' == c) { x = x + map.get(c) * numValue; }else { y = y + map.get(c) * numValue; } } catch(Exception e) { continue; } } } } System.out.println(x+","+y); } }
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 str = in.nextLine(); test(str); } } public static void test(String str) { String[] strArr = str.split(";"); int a = 0; int b = 0; for (String s : strArr) { if (s.isEmpty()) { continue; } String c = s.substring(0, 1); int length = s.length(); String value = s.substring(1, length); if (!isDigit11(value)) { continue; } int step = Integer.valueOf(value); switch (c) { case "A": a -= step; break; case "S": b -= step; break; case "W": b += step; break; case "D": a += step; break; } } System.out.println(a + "," + b); } public static boolean isDigit11(String value) { if (value.equals(" ") || value.isEmpty()) { return false; } for (int i = 0; i < value.length(); i++) { if (!Character.isDigit(value.charAt(i))) { return false; } } return true; } }
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.hasNext()) { // 注意 while 处理多个 case String[] input = in.nextLine().toLowerCase().split(";"); int x = 0; int y = 0; char letter; long num; for(String tmp:input) { if(tmp==null || tmp.length()<=0) { continue; } letter = tmp.charAt(0); try { num = Long.parseLong(tmp.substring(1)); } catch (Exception e) { continue; } switch(letter) { case 'a': x -= num; break; case 's': y -= num; break; case 'w': y += num; break; case 'd': x += num; break; default: continue; } } System.out.println(x+","+y); } } }
import java.util.Scanner; import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = br.readLine()) != null && !line.isEmpty()) { // 初始化横纵坐标 {x, y} int[] coord = {0, 0}; // 初始化移动命令 char command = 'N'; int distance = -1; // 解析数据 int offset = 0; for (int i = 0; i < line.length(); i++) { char c = line.charAt(i); // 结算! if (c == ';') { if (distance != -1) { clac(coord, command, distance); } offset = 0; distance = -1; continue; } if (offset == -1) { continue; } // 第一个字符, 默认视为命令 if (offset == 0) { command = c; offset++; continue; } // 第二个字符, 如果符合范围, 默认视为个位数 if (offset == 1 && (c >= '0' && c <= '9')) { distance = c - '0'; offset++; continue; } // 第三个字符, 如果符合范围, 可以百分百确定个位数和十位数的值 if (offset == 2 && (c >= '0' && c <= '9')) { distance = distance * 10 + (c - '0'); offset++; continue; } // 本次指令解析出现了非法指令, 暂时屏蔽 offset, 直到下一次结算 ';' 时再重新恢复 offset = -1; // 本次指令解析出现了非法指令, 重置 distance distance = -1; } System.out.println(coord[0] + "," + coord[1]); } } public static void clac(int[] coord, char command, int distance) { switch (command) { case 'A': coord[0] = coord[0] - distance; break; case 'D': coord[0] = coord[0] + distance; break; case 'W': coord[1] = coord[1] + distance; break; case 'S': coord[1] = coord[1] - distance; } } }总共提交结果:答案正确