题解 | #坐标移动#

坐标移动

https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29

解题思路

  1. 从(0,0)点开始,根据输入的指令序列移动坐标
  2. 对输入字符串按分号分割成单个指令
  3. 对每个指令进行合法性验证:
    • 第一个字符必须是A/D/W/S
    • 后面必须是1-2位的数字
  4. 根据方向更新坐标:
    • A: x减去数字
    • D: x加上数字
    • W: y加上数字
    • S: y减去数字
  5. 输出最终坐标

代码

#include <iostream>
#include <string>
#include <vector>
using namespace std;

bool isValidCommand(string& cmd) {
    if (cmd.length() < 2 || cmd.length() > 3) return false;
    if (cmd[0] != 'A' && cmd[0] != 'D' && cmd[0] != 'W' && cmd[0] != 'S') return false;
    for (int i = 1; i < cmd.length(); i++) {
        if (!isdigit(cmd[i])) return false;
    }
    return stoi(cmd.substr(1)) <= 99;
}

int main() {
    string input;
    getline(cin, input);
    
    int x = 0, y = 0;
    string cmd;
    
    for (int i = 0; i < input.length(); i++) {
        if (input[i] == ';') {
            if (isValidCommand(cmd)) {
                int num = stoi(cmd.substr(1));
                switch(cmd[0]) {
                    case 'A': x -= num; break;
                    case 'D': x += num; break;
                    case 'W': y += num; break;
                    case 'S': y -= num; break;
                }
            }
            cmd = "";
        } else {
            cmd += input[i];
        }
    }
    // 处理最后一个命令
    if (isValidCommand(cmd)) {
        int num = stoi(cmd.substr(1));
        switch(cmd[0]) {
            case 'A': x -= num; break;
            case 'D': x += num; break;
            case 'W': y += num; break;
            case 'S': y -= num; break;
        }
    }
    
    cout << x << "," << y << endl;
    return 0;
}
import java.util.Scanner;

public class Main {
    public static boolean isValidCommand(String cmd) {
        if (cmd.length() < 2 || cmd.length() > 3) return false;
        if (!"ADWS".contains(cmd.substring(0, 1))) return false;
        try {
            int num = Integer.parseInt(cmd.substring(1));
            return num <= 99;
        } catch (NumberFormatException e) {
            return false;
        }
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] commands = sc.nextLine().split(";");
        
        int x = 0, y = 0;
        for (String cmd : commands) {
            if (!cmd.isEmpty() && isValidCommand(cmd)) {
                int num = Integer.parseInt(cmd.substring(1));
                switch (cmd.charAt(0)) {
                    case 'A': x -= num; break;
                    case 'D': x += num; break;
                    case 'W': y += num; break;
                    case 'S': y -= num; break;
                }
            }
        }
        
        System.out.println(x + "," + y);
    }
}
def is_valid_command(cmd):
    if len(cmd) < 2 or len(cmd) > 3:
        return False
    if cmd[0] not in 'ADWS':
        return False
    try:
        num = int(cmd[1:])
        return 0 <= num <= 99
    except:
        return False

def move_coordinate():
    x, y = 0, 0
    commands = input().split(';')
    
    for cmd in commands:
        if not cmd or not is_valid_command(cmd):
            continue
            
        direction = cmd[0]
        distance = int(cmd[1:])
        
        if direction == 'A':
            x -= distance
        elif direction == 'D':
            x += distance
        elif direction == 'W':
            y += distance
        elif direction == 'S':
            y -= distance
            
    print(f"{x},{y}")

if __name__ == "__main__":
    move_coordinate()

算法及复杂度

  • 算法:字符串处理 + 模拟
  • 时间复杂度:,其中n是输入字符串的长度
  • 空间复杂度:,需要存储分割后的指令列表
全部评论

相关推荐

01-23 14:54
同济大学 Java
热爱敲代码的程序媛:给你提几点【专业技能】这个模块里面可优化的地方:1.【具备JVM调优经验】可以去b站上搜一下JVM调优的视频,估计一两个小时凭你的学习能力就能掌握JVM调优的实践方面的技能。2.【MySql优化】MySql这一栏,你去b站或者找个博客看看MySql优化,学一下,如果你本身比较熟悉MySql语句的话,那基本半天时间凭你的学习能力MySql语句优化方面的技能你也能掌握个差不多。以上1,2两点主要是因为我看你专业技能大部分都说的是偏理论,没有写应用。再就是最后,你结合你的项目,想一想你的项目中哪些sql语句是可以用MySql优化的,到时候你面试的时候也好结合着说一下。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务