定義一個變量,遇到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);
}
}
}