滴滴笔试
滴滴笔试
第一行包含一个整数n,表示算式的长度,即包含n个数字和n-1个运算符。(1≤n≤100000)。
第二行包含一个含有n个非0整数和n-1个运算符的算式,整数与运算符用空格隔开,运算符包括“+,-,*,/”,整数的绝对值不超过1000。
例:
输入:
6
3 + 2 + 1 + -4 * -5 + 1
输出:
1 + 2 + 3 + -5 * -4 + 1
n=int(input())
ss=input()
s1=ss.split(' ')
s2=[s for s in s1 if s not in ['+','-','*','/']]
opt=[s for s in s1 if s in ['+','-','*','/']]
flag=1
while flag:
flag=0
for i in range(len(opt)):
if int(s2[i])>int(s2[i+1]):
if opt[i] == '+' and opt[i+1] not in ['*','/']:
s2[i],s2[i+1]=s2[i+1],s2[i]
flag=1
elif opt[i]=='*' and (i<1 or opt[i-1]!='/'):
s2[i],s2[i+1]=s2[i+1],s2[i]
flag=1
elif opt[i]=='/' and i>0 and opt[i-1]=='/':
s2[i],s2[i+1]=s2[i+1],s2[i]
flag=1
elif opt[i]=='-' and i>0 and opt[i-1]=='-':
s2[i],s2[i+1]=s2[i+1],s2[i]
flag=1
res=''
for i in range(len(opt)):
res+=s2[i]
res+=' '
res+=opt[i]
res+=' '
res+=s2[i+1]
print(res)
#滴滴##笔试题目#
