题解 | #坐标移动#

坐标移动

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static boolean isIlegal(String s){
        if(s.length()!=3&&s.length()!=2)
            return false;
        char[]ch=s.toCharArray();
        if(ch[0]!='A'&&ch[0]!='S'&&ch[0]!='W'&&ch[0]!='D')
            return false;
        if(!(ch[1]>='0'&&ch[1]<='9')){
            return false;
        }
        if(s.length()==3&&(ch[1]=='0'||!(ch[2]>='0'&&ch[2]<='9')))
            return false;
        return true;
    }
    public static int getNum(String s){
        char[]ch=s.toCharArray();
        int ans=1;
        if(s.length()==2)
            ans=ch[1]-'0';
        if(s.length()==3)
            ans=(ch[1]-'0')*10+ch[2]-'0';
        return ans;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int x=0,y=0;
        String s=in.next();
        String []newStr=s.split(";");
        for(int i=0;i<newStr.length;i++){
            if(isIlegal(newStr[i])){
                char ch=newStr[i].charAt(0);
                if(ch=='A')
                 x=x-getNum(newStr[i]);
                else if(ch=='D')
                x=x+getNum(newStr[i]);
                else if(ch=='W')
                y=y+getNum(newStr[i]);
                else y=y-getNum(newStr[i]);
            }
        }
        System.out.print(x+","+y);
    }
}

全部评论

相关推荐

07-15 11:41
门头沟学院 Java
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-15 17:17
听说过付费实习,没想到这么贵啊我去,要不我给你个腰子吧
哈哈哈,你是老六:这种公司一定要注意啊,不要随便签合同,只要签了后面钱可能回不来,而且你通过法律途径也弄不回
点赞 评论 收藏
分享
嵐jlu:我是山川🐔里🐔🧱的,阿里系简历全过; 你这简历一看就还是半成品啊,没有荣誉经历奖项什么的吗?
投递阿里巴巴集团等公司8个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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