定义一个变量,遇到L自增,遇到R自减;(抵消左右转操作)
遍历完成后,该变量为n,n为正表示向左转n步,为负表示向右转n步;
然后将n对4取余(每转四步相当于没转);
最后输出结果
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()) {
int n = in.nextInt();
int a = 0;
char res = 'N';
in.nextLine();
String s = in.nextLine();
for(int i = 0;i < s.length();i++) {
if(s.charAt(i) == 'L')a++;
else a--;
}
if(a == 0) res = 'N';
else if(a < 0) {
a = (-a & (4 - 1));
switch (a) {
case 0 :
res = 'N';
break;
case 1 :
res = 'E';
break;
case 2 :
res = 'S';
break;
case 3 :
res = 'W';
break;
}
}else {
a = a & (4 - 1);
switch (a) {
case 0 :
res = 'N';
break;
case 1 :
res = 'W';
break;
case 2 :
res = 'S';
break;
case 3 :
res = 'E';
break;
}
}
System.out.println(res);
}
}
}