每个输入包含一个测试用例。
每个测试用例的第一行包含一个正整数,表示转方向的次数N(N<=1000)。
接下来的一行包含一个长度为N的字符串,由L和R组成,L表示向左转,R表示向右转。
输出牛牛最后面向的方向,N表示北,S表示南,E表示东,W表示西。
3 LRR
E
import sys n = int(sys.stdin.readline().strip()) s = sys.stdin.readline().strip() res = 0 for i in range(n): if s[i] == "L": res -= 1 else: res += 1 res %= 4 direction = ['N','E','S','W'] print(direction[res])
def main(): dict = {'EL': 'N','ER': 'S', 'SL': 'E', 'SR': 'W', 'WL': 'S', 'WR': 'N', 'NL': 'W', 'NR': 'E'} N = int(input()) S = input() toward = 'N' for s in S: cur = toward + s toward = dict[cur] print(toward) if __name__ == '__main__': main()
if __name__ == "__main__": orientation = ['W','N','E','S'] #定义四个方向 num = int(input()) direction = input() j = 1 for i in range(len(direction)): if(direction[i]=='L'): j = j-1 if(j==-1): #方向边界及时调整 j=3 else: j = j+1 if(j==4): #方向边界调整 j=0 print(orientation[j])
## 向右转则+1,向左转-1,最后的数%4=0为N,%4=1为E,%4=2为S,%4=3为W import sys n=int(sys.stdin.readline().strip()) rotate=sys.stdin.readline().strip() direction=['N','E','S','W'] index=rotate.count('R')-rotate.count('L') print(direction[index%4])
try: while True: N = int(input()) s = input() res = 0 s = list(s) for char in s: if char == 'L': res -= 1 else: res += 1 if res < 0: res = -(abs(res) % 4) else: res = res % 4 if res == 0: print('N') elif res == 3 or res == -1: print('W') elif res == 2 or res == -2: print('S') else: print('E') except: pass
def pos(face_position, return_position): if face_position == 'N' and return_position == 'L': return 'W' elif face_position == 'N' and return_position == 'R': return 'E' elif face_position == 'S' and return_position == 'L': return 'E' elif face_position == 'S' and return_position == 'R': return 'W' elif face_position == 'W' and return_position == 'L': return 'S' elif face_position == 'W' and return_position == 'R': return 'N' elif face_position == 'E' and return_position == 'L': return 'N' elif face_position == 'E' and return_position == 'R': return 'S' n = int(input().strip()) way = [i for i in input().strip()] direction0 = 'N' a = pos(direction0, way[0]) for i in range(1, len(way)): a = pos(a, way[i]) print(a)
case通过率100%,不是最聪明的解法,但是是最直观的解法
首先定义函数pos(face_position, return_position)
face_position代表当***向,return_position代表当前的转向
初始方向为direction0 = 'N'
第一步a = pos(direction0, way[0])
第二步a = pos(a, way[1])
一直到最后一步a = pos(a, way[n])
当前步的朝向受上一步朝向和转向的影响
""" 左转减1,右转加1 结果 mod 4,打印方位 """ import sys if __name__ == "__main__": # sys.stdin = open('input.txt', 'r') n = int(input().strip()) s = input().strip() ans = 0 for i in range(len(s)): if s[i] == 'L': ans -= 1 else: ans += 1 point = ['N', 'E', 'S', 'W'] print(point[ans % 4])