在一行上输入一个长度
,仅由可见字符构成的字符串
,代表输入的指令序列。
在一行上输出一个两个整数,代表小人最终位置的横纵坐标,使用逗号间隔。
A10;S20;W10;D30;X;A1A;B10A11;;A10;
10,-10
对于这个样例,我们模拟小人的移动过程:
第一个指令
是合法的,向左移动
个单位,到达
点;
第二个指令
是合法的,向下移动
个单位,到达
点;
第三个指令
是合法的,向上移动
个单位,到达
点;
第四个指令
是合法的,向右移动
个单位,到达
点;
第五个指令
不合法,跳过;
第六个指令
不合法,跳过;
第七个指令
不合法,跳过;
第八个指令
不合法,跳过;
第九个指令
是合法的,向左移动
个单位,到达
点。
ABC;AKL;DA1;
0,0
while True: try: s = input() s = list(s.split(";")) x,y = 0,0 for c in s: if not c: continue elif c[0]=='A' and c[1:].isdigit(): x-=int(c[1:]) elif c[0]=='S' and c[1:].isdigit(): y-=int(c[1:]) elif c[0]=='W' and c[1:].isdigit(): y+=int(c[1:]) elif c[0]=='D' and c[1:].isdigit(): x+=int(c[1:]) print(str(x)+','+str(y)) except: break
a = input().split(';') b = 0 c = 0 for i in a: if len(i) == 0: continue elif 'A' == i[0] and i.count('A') == 1 and i[1:].isdigit(): b -= int(i[1:]) elif 'D' == i[0] and i.count('D') == 1 and i[1:].isdigit(): b += int(i[1:]) elif 'W' == i[0] and i.count('W') == 1 and i[1:].isdigit(): c += int(i[1:]) elif 'S' == i[0] and i.count('S') == 1 and i[1:].isdigit(): c -= int(i[1:]) print(b,end=',') print(c)好像显得我很low
import re points = [p.strip() for p in input().split(';')] points = [p for p in points if re.match('^[A|D|W|S]\\d{1,2}$', p)] x,y = 0,0 for p in points: if p.startswith('A'): x -= int(p[1:]) elif p.startswith('D'): x += int(p[1:]) elif p.startswith('W'): y += int(p[1:]) else: y -= int(p[1:]) print(f'{x},{y}')
x,y=0,0 a=input().split(';') for i in a: try: int(i[1:]) except: continue else: if i[0]=='A': x=x-int(i[1:]) if i[0]=='D': x=x+int(i[1:]) if i[0]=='W': y=y+int(i[1:]) if i[0]=='S': y=y-int(i[1:]) print(str(x)+','+str(y))
def yd(str): ff=str.split(";",) sc=[0,0] num=len(ff) for i in range(num): if ff[i].startswith('A') and ff[i][1:].isdigit(): sc[0]=sc[0]-int(ff[i][1:]) elif ff[i].startswith('D') and ff[i][1:].isdigit(): sc[0]=sc[0]+int(ff[i][1:]) elif ff[i].startswith('W') and ff[i][1:].isdigit(): sc[1]=sc[1]+int(ff[i][1:]) elif ff[i].startswith('S') and ff[i][1:].isdigit(): sc[1]=sc[1]-int(ff[i][1:]) else: continue print("{0},{1}".format(sc[0],sc[1])) while True: try: a=input() yd(a) except: break
import re pattern=re.compile(r'^[ASWD]\d{2}$') s=input() x=0 y=0 for i in s.split(";"): se=re.search(pattern,i) if se : if i[0] == "A" : x -= int(i[1:3]) if i[0] == "D" : x += int(i[1:3]) if i[0] == "W" : y += int(i[1:3]) if i[0] == "S" : y -= int(i[1:3]) print("%d,%d"%(x,y))为啥不通过?求大神指教,找了半天没找到原因。
a=input() L=a.split(';') start=[0,0] #x,y=0,0 for s in L: #if len(s)>1 and s[0] in ['A','D','W','S'] and set(s[1:]).issubset(set("1234567890")): #先判断是否长度大于1(排除空值和单个字符),再判断首字符是否为ADWS,最后判断后面的字符是否为数字 if len(s)>1 and s[0] in ['A','D','W','S'] and s[1:].isdigit(): if s.startswith('A'):#是否以A开头 start[0]-=int(s[1:]) elif s[0]=='D': start[0]+=int(s[1:]) elif s[0]=='W': start[1]+=int(s[1:]) elif s[0]=='S': start[1]-=int(s[1:]) else: continue print (start[0],start[1],sep=',')
import sys for ords in sys.stdin: lst = ords.split(';') x = y = 0 for i in lst: if 0 < len(i) <= 3 and i[0] in 'ADWS' and i[1:].isdigit(): n = int(i[1:]) if i[0] == 'A': x -= n elif i[0] == 'D': x += n elif i[0] == 'W': y += n else: y -= n print('{:d},{:d}'.format(x,y))
locations = input().split(";") clean_location = [] ways = {"A":(-1,0),"D":(1,0),"W":(0,1),"S":(0,-1)} for location in locations: if len(location) > 3: continue if len(location) == 3&nbs***bsp;len(location) == 2: if location[0] in ways and location[1:].isdigit(): clean_location.append(location) initial = [0,0] for i in clean_location: way = ways[i[0]] num = int(i[1:]) initial[0] += way[0]*num initial[1] += way[1]*num print("{},{}".format(initial[0],initial[1]))
简单的python代码实现
a = input().split(str(';')) x,y=0,0 for i in range(len(a)): try: if a[i][0] in "ASDW" : if a[i][0]=="A": x-=int(a[i][1:]) if a[i][0]=="S": y-=int(a[i][1:]) if a[i][0]=="D": x+=int(a[i][1:]) if a[i][0]=="W": y+=int(a[i][1:]) except: continue print("{},{}".format(x,y))