题解 | #坐标移动#

坐标移动

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        in.useDelimiter("\n");
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int[] xy = new int[] {0, 0};
            String s = in.next();
            String[] split = s.split(";");
            for (String move : split) {
                if (null == move || "".equals(move) || move.length() < 2) {
                    continue;
                }
                char dir = move.charAt(0);
                String distance = move.substring(1);
                int distanceInt = 0;
                try {
                    distanceInt = Integer.parseInt(distance);
                } catch (Exception e) {
                    continue;
                }

                if ('A' == dir) {
                    xy[0] -= distanceInt;
                    continue;
                }
                if ('S' == dir) {
                    xy[1] -= distanceInt;
                    continue;
                }
                if ('W' == dir) {
                    xy[1] += distanceInt;
                    continue;
                }
                if ('D' == dir) {
                    xy[0] += distanceInt;
                }
            }
            System.out.println(xy[0] + "," + xy[1]);
        }
    }
}

全部评论

相关推荐

认真搞学习:28小登的建议,投算法岗不要写什么物理竞赛,互联网+,多写点项目,用什么算法做了什么。还有本科算法是不可能的开发你这个也没有项目啊
点赞 评论 收藏
分享
06-07 19:59
门头沟学院 C++
补药卡我啊😭:都快15年前的了还在11新特性
你的简历改到第几版了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务