题解 | #坐标移动#

坐标移动

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

import java.util.*;
import java.util.regex.*;

public class Main {
    public static void main(String[] args) 
	{
		Scanner sr = new Scanner(System.in);
		while(sr.hasNext())
		{
		int x = 0;//横坐标
        int y = 0;//纵坐标
		String str=sr.nextLine();
		String[] ch_str=str.split(";");
        String reg="[WASDwasd]\\d{1,2}";
		for (String s:ch_str )
		{
			if (Pattern.matches(reg,s))
			{
				String str1=s.replaceAll("[WDwd]","+").replaceAll("[ASas]","-");
				if (Pattern.matches("[ADad]\\d{1,2}",s))
				{
					x+=Integer.parseInt(str1);
				}
				else 
					y+=Integer.parseInt(str1);
			}
		}
		System.out.println(x+","+y);
		}
		sr.close();
    }
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务