题解 | #坐标移动#

坐标移动

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

import java.util.Scanner;

public class Main { public static void main(String[] args) { int x = 0; int y = 0; Scanner in = new Scanner(System.in); String str = ""; // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case

		str += in.nextLine();
		System.out.println(str);
	}
	String arr[] = str.split(";");
	for (String s : arr) {
		if (s.length() <= 3 && s.length() >= 2) {
			char c = s.charAt(0);
			int xx = 0;
			int yy = 0;
			if (c == 'A' || c == 'S' || c == 'W' || c == 'D') {
			
				if (s.length() == 3) {

					if (s.charAt(1) >= '0' && s.charAt(1) <= '9' && s.charAt(2) >= '0' && s.charAt(2) <= '9') {
						int aa = Integer.valueOf("" + s.charAt(1) + s.charAt(2));
						if (c == 'A') {
							xx = -1 * aa;
							yy = 0;
						} else if (c == 'S') {
							xx = 0;
							yy = -1 * aa;
						} else if (c == 'W') {
							xx = 0;
							yy = aa;
						} else if (c == 'D') {
							xx = aa;
							yy = 0;
						}
					}
				}
				if (s.length() == 2) {

					if (s.charAt(1) >= '0' && s.charAt(1) <= '9') {
						int aa = Integer.valueOf("" + s.charAt(1));
						if (c == 'A') {
							xx = -1 * aa;
							yy = 0;
						} else if (c == 'S') {
							xx = 0;
							yy = -1 * aa;
						} else if (c == 'W') {
							xx = 0;
							yy = aa;
						} else if (c == 'D') {
							xx = aa;
							yy = 0;
						}
					}
				}
			}
			x = x+xx;
			y = y+yy;
		}
	}
	System.out.println(x+","+y);
}

}

全部评论

相关推荐

四道编程、模拟两道、动态规划、二分查找,大家答得怎么样?
bowlec:第四题直接排序然后忘了可以二分所以直接遍历查找,居然0通过显示答案错误而不是超时,这是思路错了吗暴力也不至于0通过吧😭
投递携程等公司10个岗位 > 携程求职进展汇总
点赞 评论 收藏
分享
02-16 13:52
门头沟学院 Java
给🐭🐭个面试机会吧:嘿,mvbatis
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务