題目描述
牛牛去犇犇老師家補課,出門的時候面向北方,但是現在他迷路了。雖然他手裏有一張地圖,但是他需要知道自己面向哪個方向,請你幫幫他。
輸入描述:
每個輸入包含一個測試用例。 每個測試用例的第一行包含一個正整數,表示轉方向的次數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");
}
}
}
}