在一行上输入一个长度
,仅由可见字符构成的字符串
,代表输入的指令序列。
在一行上输出一个两个整数,代表小人最终位置的横纵坐标,使用逗号间隔。
A10;S20;W10;D30;X;A1A;B10A11;;A10;
10,-10
对于这个样例,我们模拟小人的移动过程:
第一个指令
是合法的,向左移动
个单位,到达
点;
第二个指令
是合法的,向下移动
个单位,到达
点;
第三个指令
是合法的,向上移动
个单位,到达
点;
第四个指令
是合法的,向右移动
个单位,到达
点;
第五个指令
不合法,跳过;
第六个指令
不合法,跳过;
第七个指令
不合法,跳过;
第八个指令
不合法,跳过;
第九个指令
是合法的,向左移动
个单位,到达
点。
ABC;AKL;DA1;
0,0
import re #正则表达式 FangXiang = {'W':0, 'A':0, 'S':0, 'D':0} #字典用来存储有效输入 ShuRu = input().split(';') ZhengLi = [] pattern = r'\b[WASD]\d+\b' #用正则表达式筛选出合适的输入 for i in ShuRu: if re.match(pattern,i): ZhengLi.append(i) for i in ZhengLi:#将整理后的输入放进字典里 if i[0] in FangXiang: FangXiang[i[0]] += int(i[1:]) #使用字典里的记录来计算最后坐标 print(f"{FangXiang['D']- FangXiang['A']},{FangXiang['W']-FangXiang['S']}")
import sys import re moves = input().strip().split(';') pattern = re.compile(r'^[ADWS]\d{1,2}$') for move in moves[:]: matches = re.findall(pattern,move) # print(matches) if not matches&nbs***bsp;move != matches[0]: moves.remove(move) # print(moves) x = 0 y = 0 for move in moves: # print(x,y) if move[0] == 'A': x -= int(move[1:]) elif move[0] == 'D': x += int(move[1:]) elif move[0] == 'W': y += int(move[1:]) elif move[0] == 'S': y -= int(move[1:]) print(x,y,sep=',')
start_point = [0,0] coordinates = input("").split(";") for coordinate in coordinates: if coordinate[:1]=="A" and coordinate[1:].isdigit(): start_point[0] -= int(coordinate[1:]) elif coordinate[:1]=="D" and coordinate[1:].isdigit(): start_point[0] += int(coordinate[1:]) elif coordinate[:1]=="W" and coordinate[1:].isdigit(): start_point[1] += int(coordinate[1:]) elif coordinate[:1]=="S" and coordinate[1:].isdigit(): start_point[1] -= int(coordinate[1:]) else: continue print("{},{}".format(start_point[0],start_point[1]))
#不用isdigit的方法 a = input().split(";") num1 = "ASWD" num2 = "0123456789" i, j = 0, 0 for ii in a: if ii and ii[0] in num1: for jj in range(1, len(ii)): if ii[jj] not in num2: break elif jj == len(ii)-1: if ii[0] == "A": i -= int(ii[1:]) elif ii[0] == "S": j -= int(ii[1:]) elif ii[0] == "W": j += int(ii[1:]) else: i += int(ii[1:]) print(str(i)+","+str(j))
st = input().split(';') num1=num2=0 for s in st: if s[1:].isnumeric(): if s[0] == 'A': s = int(s[1:]) num1 -= s continue if s[0] == 'D': s = int(s[1:]) num1 += s continue if s[0] == 'W': s = int(s[1:]) num2 += s continue if s[0] == 'S': s = int(s[1:]) num2 -= s continue print(f'{num1},{num2}')
a=input().split(';') b=[0,0] c=['A','S','W','D'] for i in a: if len(i)==3&nbs***bsp;len(i)==2: if i[0].upper() in c: if i[1:].isdigit(): if i[0]=='A': b[0]=int(b[0])-int(i[1:]) elif i[0]=='D': b[0]=int(b[0])+int(i[1:]) elif i[0]=='W': b[1]=int(b[1])+int(i[1:]) elif i[0]=='S': b[1]=int(b[1])-int(i[1:]) print(f'{b[0]},{b[1]}')
Coordinate = input().replace(';',' ').split() X,Y = 0,0 verify = ['A','D','W','S'] for word in range(len(Coordinate)): Move = Coordinate[word] if len(Move) <= 3 and len(Move) > 1 and Move[0] in verify and Move[1:].isdigit(): if Move[0] == verify[0]: X -= int(Move[1:]) if Move[0] == verify[1]: X += int(Move[1:]) if Move[0] == verify[2]: Y += int(Move[1:]) if Move[0] == verify[3]: Y -= int(Move[1:]) print('%d,%d' %(X,Y))