题目描述
牛牛去犇犇老师家补课,出门的时候面向北方,但是现在他迷路了。虽然他手里有一张地图,但是他需要知道自己面向哪个方向,请你帮帮他。
输入描述:
每个输入包含一个测试用例。 每个测试用例的第一行包含一个正整数,表示转方向的次数N(N<=1000)。 接下来的一行包含一个长度为N的字符串,由L和R组成,L表示向左转,R表示向右转。
输出描述:
输出牛牛最后面向的方向,N表示北,S表示南,E表示东,W表示西。
示例1
输入
3 LRR
输出
E
代码:
// 本题为考试单行多行输入输出规范示例,无需提交,不计分。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {// 注意,如果输入是多个测试用例,请通过while循环处理多个测试用例
String N=in.nextLine();
String string=in.nextLine();
//0N,1E,2S,3W
int count=0;
char[] ch=string.toCharArray();
for(int i=0;i<ch.length;i++)
{
if(ch[i]=='L')
{
count--;
if(count==-1)
{
count=3;
}
}if(ch[i]=='R')
{
count++;
if(count==4)
{
count=0;
}
}
}
if(count==0)
{
System.out.println("N");
}else if(count==1)
{
System.out.println("E");
}else if(count==2)
{
System.out.println("S");
}else
{
System.out.println("W");
}
}
}
}