在一行上输入一个长度
,由大写字母、数字和分号(
)构成的字符串
,代表输入的指令序列。保证字符串中至少存在一个
,且末尾一定为
。
在一行上输出一个两个整数,代表小人最终位置的横纵坐标,使用逗号间隔。
A10;S20;W10;D30;X;A1A;B10A11;;A10;
10,-10
对于这个样例,我们模拟小人的移动过程:
第一个指令
是合法的,向左移动
个单位,到达
点;
第二个指令
是合法的,向下移动
个单位,到达
点;
第三个指令
是合法的,向上移动
个单位,到达
点;
第四个指令
是合法的,向右移动
个单位,到达
点;
第五个指令
不合法,跳过;
第六个指令
不合法,跳过;
第七个指令
不合法,跳过;
第八个指令
不合法,跳过;
第九个指令
是合法的,向左移动
个单位,到达
点。
ABC;AKL;DA1;D001;W023;A100;S00;
0,0
在这个样例中,全部指令均不合法,因此小人不移动。
A00;S01;W2;
0,1
本题已于下方时间节点更新,请注意题解时效性:
1. 2025-05-15 更新题面,新增几组hack数据(暂未进行重测)。
2. 2024-12-16 更新题面。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
String [] arr = str.split(";");
int x = 0, y = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i].matches("[A][0-9]{1,2}")) {
x = x - Integer.parseInt(arr[i].substring(1));
} else if (arr[i].matches("[D][0-9]{1,2}")) {
x = x + Integer.parseInt(arr[i].substring(1));
} else if (arr[i].matches("[W][0-9]{1,2}")) {
y = y + Integer.parseInt(arr[i].substring(1));
} else if (arr[i].matches("[S][0-9]{1,2}")) {
y = y - Integer.parseInt(arr[i].substring(1));
}
}
System.out.println(x + "," + y);
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s1 = in.next();
String[] sA= s1.split(";");
int x = 0,y=0;
for(String s : sA){
if(s.length() <=1){
continue;
}
if(!isNumeric(s.substring(1))){
continue;
}
if(s.indexOf("A") == s.lastIndexOf("A") && s.indexOf("A") == 0){
x -= Long.valueOf(s.substring(1));
} else if(s.indexOf("S") == s.lastIndexOf("S")&& s.indexOf("S") == 0){
y -= Long.valueOf(s.substring(1));
}else if(s.indexOf("W") == s.lastIndexOf("W")&& s.indexOf("W") == 0){
y += Long.valueOf(s.substring(1));
}else if(s.indexOf("D") == s.lastIndexOf("D")&& s.indexOf("D") == 0){
x+= Long.valueOf(s.substring(1));
}
}
System.out.println(x + "," + y);
}
public static boolean isNumeric(String s) {
return s != null && s.matches("\\d+");
}
}
检查字符串长度
检查后面的字符串是否是数字
检查AWDS字符串第一次最后一次的角标是否一样且等于0 import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static int func(String s) {
if (s == null) {
return 0;
}
try {
return Integer.parseInt(s.trim());
} catch (NumberFormatException e) {
return 0;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
String[] arr = s.split(";");
int countA = 0;
int countD = 0;
int countW = 0;
int countS = 0;
for (int i = 0; i < arr.length; i++) {
// 确保字符串长度至少为2且第一个字符是预期字符
if (arr[i].length() >= 2 && (arr[i].charAt(0) == 'A'
|| arr[i].charAt(0) == 'D'
|| arr[i].charAt(0) == 'W'
|| arr[i].charAt(0) == 'S')) {
if (arr[i].charAt(0) == 'A') {
countA += func(arr[i].substring(1));
} else if (arr[i].charAt(0) == 'D') {
countD += func(arr[i].substring(1));
} else if (arr[i].charAt(0) == 'W') {
countW += func(arr[i].substring(1));
} else if (arr[i].charAt(0) == 'S') {
countS += func(arr[i].substring(1));
}
}
}
int x = 0 - countA + countD;
int y = 0 + countW - countS;
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);
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);
}
}