每个输入包含一个测试用例。
每个测试用例的第一行包含一个正整数,表示转方向的次数N(N<=1000)。
接下来的一行包含一个长度为N的字符串,由L和R组成,L表示向左转,R表示向右转。
输出牛牛最后面向的方向,N表示北,S表示南,E表示东,W表示西。
3 LRR
E
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
const string status = "NESW";
while (cin >> n) {
string s;
cin >> s;
int i = 0;
for (char &c : s) {
if (c == 'L')
i--;
else
i++;
if (i == -1 || i == 4)
i = (i + 4) % 4;
}
cout << status[i] << endl;
}
return 0;
}
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int i = scanner.nextInt(); String next = scanner.next(); char[] dict={'W','E','S','N'}; char[] chars = next.toCharArray(); //System.out.println(i); //System.out.println(chars); int a=0; for (int j = 0; j < chars.length; j++) { if (chars[j] == 'L') { a-=1; } else { a+=1; } } System.out.println(a); if (a%4 <0) { System.out.println(dict[(a%4)+4]); }else { System.out.println(dict[(a%4)]); } // } }
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class FindDirections { public static void main(String[] args) { Map<Integer,String> map = new HashMap<>(); map.put(0,"N"); map.put(-1,"E"); map.put(-2,"S"); map.put(-3,"W"); map.put(1,"W"); map.put(2,"S"); map.put(3,"E"); Scanner sc = new Scanner(System.in); String turnTimes = sc.nextLine(); String turnDirect = sc.nextLine(); char[] directionTimes = turnDirect.toCharArray(); int countL = 0; int countR = 0; // System.out.println(directionTimes[0] == 'L'); for(int i = 0 ; i <= directionTimes.length - 1 ; i++) { if (directionTimes[i] == 'L') countL++; else countR++; } int dist = (countL - countR)%4; System.out.println(map.get(dist)); } }
function main(){ var n = readline();//获取转方向的次数 var str = readline();//获取转的方向 var arr = str.split(""); var position = ['N','E','S','W']; var count = 0; for(var i=0; i<n; i++){ if(arr[i] === 'L' && count === 0){ count = 3; }else if(arr[i] === 'L'){ count --; }else if(arr[i] === 'R' && count === 3){ count = 0; }else if(arr[i] === 'R'){ count ++; } } console.log(position[count]); } main();