牛牛去犇犇老师家补课,出门的时候面向北方,但是现在他迷路了。虽然他手里有一张地图,但是他需要知道自己面向哪个方向,请你帮帮他。
输入描述:
每个输入包含一个测试用例。
每个测试用例的第一行包含一个正整数,表示转方向的次数N(N<=1000)。
接下来的一行包含一个长度为N的字符串,由L和R组成,L表示向左转,R表示向右转。
输出描述:
输出牛牛最后面向的方向,N表示北,S表示南,E表示东,W表示西。
输入例子1:
3
LRR
输出例子1:
E
#include <bits/stdc++.h>
using namespace std;
char diretion(char &c,char &dire) {
string status = "NESW";
int n = (c == 'L') ? -1 : 1;
int pos = status.find(dire);
return status[(pos + n + 4) % 4];
}
int main() {
int n;
//freopen("input.txt", "r", stdin);
while (cin >> n) {
string s;
char dire = 'N';
cin >> s;
for (char c : s) {
dire = diretion(c, dire);
}
cout << dire << endl;
}
return 0;
}
简化一点:
#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;
}