c++题解
迷路的牛牛
http://www.nowcoder.com/questionTerminal/fc72d3493d7e4be883e931d507352a4a
看到这题,我最先想到是时钟旋转的360度。
- N: 0度
- E: 90度
- S: 180度
- W: 270度
向左旋转L,即是-90
向右旋转R,即是+90
为了防止负数和值过大,需要 p = (p +/- 90 + 360) % 360
代码如下:
#include <iostream>
using namespace std;
int main() {
int n, p = 0;
char x;
cin >> n;
while (n--) {
cin >> x;
if (x == 'L')
p = (p - 90 + 360) % 360;
else
p = (p + 90 + 360) % 360;
}
if (p == 0)
cout << 'N' << endl;
else if (p == 90)
cout << 'E' << endl;
else if (p == 180)
cout << 'S' << endl;
else
cout << 'W' << endl;
return 0;
} 