题解 | #坐标移动#

坐标移动

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);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String a = in.nextLine();
            if(a.length() <= 0 || a.length() > 10000){
                //System.out.println("0,0");
            }
            String[] arr  = a.split(";");
            int[][] aint = {{0,0}};
            for(int i = 0; i < arr.length; i++){
                String ar = arr[i];
                if(!"".equals(ar.trim())){
                    String b = ar.substring(0,1);
                    String c = ar.substring(1,ar.length());
                    try{
                        int d = Integer.valueOf(c);
                        aint = updateArray(aint,b,d);
                    }catch(Exception e){
                        //System.out.println("0,0");
                    }
                }
            }
            System.out.println(aint[0][0]+","+aint[0][1]);
        }
    }

    public static int[][] updateArray(int[][] a,String  b, int c){
        if("A".equals(b)){
            a[0][0] = a[0][0] - c ;
        }else if("D".equals(b)){
            a[0][0] = a[0][0] + c ;
        }else if("W".equals(b)){
            a[0][1] = a[0][1] + c ;
        }else if("S".equals(b)){
            a[0][1] = a[0][1] - c ;
        }else{
            //System.out.println("0,0");
        }
        return a;
    }
}

全部评论

相关推荐

03-25 19:00
东北大学 Java
程序员牛肉:太好了,是聊天记录。不得不信了。 当个乐子看就好,不要散播焦虑
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
03-18 14:29
牛客604067584号:感觉算法卷的人少很多,毕竟只有一部分bg还不错的硕士才会考虑算法,虽然hc不如后端,但是竞争真的少很多。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务