滴滴,8月27日秋招 算法 笔试 第一题
import copy def swap(s, i, j): temp = s[i] s[i] = s[j] s[j] = temp return True if __name__ == "__main__": n = int(input()) s = [x for x in input().strip().split(' ')] # print(s) while True: s_ = copy.deepcopy(s) l = 0 r = 2 while r < len(s): if s[l+1] == '-' or s[l+1] == '/': l += 2 r += 2 continue if s[l+1] == '+': if l == 0 or r == len(s)-1: if l == 0 and (s[r+1] == '+' or s[r+1] == '-') and int(s[l]) > int(s[r]): swap(s, l, r) if r == len(s)-1 and (s[l-1] == '+' or s[l-1] == '-') and int(s[l]) > int(s[r]): swap(s, l, r) else: if (s[l-1] == '+' or s[l-1] == '-') and (s[r+1] == '+' or s[r+1] == '-') and int(s[l]) > int(s[r]): swap(s, l, r) if s[l+1] == '*': if l == 0 and int(s[l]) > int(s[r]): swap(s, l, r) if r == len(s)-1 and s[l-1] != '/' and int(s[l]) > int(s[r]): swap(s, l, r) if s[l-1] != '/' and int(s[l]) > int(s[r]): swap(s, l, r) l += 2 r += 2 if s_ == s: break print(' '.join(s))
#滴滴##秋招##笔试题目##题解#