迷路的牛牛-网易2019实习编程题目

/*
 * 4.迷路的牛牛
 * 时间限制:1秒
 * 空间限制:32768K
 * 牛牛去犇犇老师家补课,出门的时候面向北方,但是现在他迷路了。虽然他手里有一张地图,
 * 但是他需要知道自己面向哪个方向,请你帮帮他。
 * 输入描述:
 * 每个输入包含一个测试用例。
 * 每个测试用例的第一行包含一个正整数,表示转方向的次数N(N<=1000)。
 * 接下来的一行包含一个长度为N的字符串,由L和R组成,L表示向左转,R表示向右转。
 * 输出描述:
 * 输出牛牛最后面向的方向,N表示北,S表示南,E表示东,W表示西。
 * 输入例子1:
 * 3
 * LRR
 * 输出例子1:
 * E

 */

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// 构造一个环
		DirectionNode node1 = new DirectionNode('N');
		DirectionNode node2 = new DirectionNode('E');
		DirectionNode node3 = new DirectionNode('S');
		DirectionNode node4 = new DirectionNode('W');
		node1.right = node2;
		node1.left = node4;
		node2.left = node1;
		node2.right = node3;
		node3.left = node2;
		node3.right = node4;
		node4.left = node3;
		node4.right = node1;
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		String st = in.nextLine(); // 加多一个nextLine防着回车结束
		String str = in.nextLine();
		char[] directions = str.toCharArray();
		for (int i = 0; i < N; i++) {
			if (directions[i] == 'L') {
				// 向左转
				node1 = node1.left;
			} else {
				// 向右转
				node1 = node1.right;
			}
		}
		System.out.println(node1.direction);
	}

}
class DirectionNode {
	char direction;
	DirectionNode left;
	DirectionNode right;
	public DirectionNode(char direction) {
		this.direction = direction;
	}
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值